ASP.NET Core使用JWT认证授权的方法

2025-05-29 0 32

demo地址: https://github.com/william0705/JWTS

名词解析

认证 : 识别用户是否合法

授权: 赋予用户权限 (能访问哪些资源)

鉴权: 鉴定权限是否合法

Jwt优势与劣势

优势

1、 无状态

token 存储身份验证所有信息 , 服务端不需要保存用户身份验证信息, 减少服务端压力 , 服务端更容易水平扩展, 由于无状态, 又会导致它最大缺点 , 很难注销

2、 支持跨域访问

Cookie是不允许垮域访问的,token支持

3、 跨语言

基于标准化的 JSON Web Token (JWT) , 不依赖特定某一个语言 , 例如生成的Token可以对多种语言使用(Net , Java , PHP …)

劣势

1、Token有效性问题

后台很难注销已经发布的Token , 通常需要借助第三方储存(数据库/缓存) 实现注销, 这样就会失去JWT最大的优势

2、占带宽

Token长度(取决存放内容) 比session_id大 , 每次请求多消耗带宽 , token只存必要信息 , 避免token过长

3、需要实现续签

cookies – session 通常是框架已经实现续签功能, 每次访问把过期时间更新, JWT需要自己实现, 参考OAuth2刷新Token机制实现刷新Token

4、消耗更多CPU

每次请求需要对内容解密和验证签名这两步操作,典型用时间换空间

只能根据自身使用场景决定使用哪一种身份验证方案 , 没有一种方案是通用的,完美的

.NET Core集成JWT认证授权服务

1、认证服务API:认证用户,并发布Token

1、引入nuget包,System.IdentityModel.Tokens.Jwt
2、创建生成Token的服务,建议使用面向接口和实现编程,方便服务注入容器ServicesCollection(涉及DI和IOC概念)
3、创建接口

?

1

2

3

4

5

6

7

8

9

10

11

12

13
namespace JWTS.Services

{

public interface IJWTService

{

/// <summary>

/// 根据验证通过后的用户以及角色生成Token,以达到角色控制的作用

/// </summary>

/// <param name="userName"></param>

/// <param name="role"></param>

/// <returns></returns>

string GetToken(string userName,string role);

}

}

4、在appsettings.config中添加生成token需要的信息,并映射成对象

?

1

2

3

4

5

6

7

8

9

10

11

12
"TokenParameter": {

"Issuer": "William", //这个JWT的签发主体(发行者)

"Audience": "William", //这个JWT的接收对象

"SecurityKey": "askalsnlkndhasnaslkasmadka"

}

  public class TokenParameter

  {

    public string Issuer { get; set; }

    public string Audience { get; set; }

    public string SecurityKey { get; set; }

  }

5、实现接口,注入Configuration,获取TokenParameter对象

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56
using Microsoft.Extensions.Configuration;

using System;

using System.IdentityModel.Tokens.Jwt;

using System.Security.Claims;

using System.Text;

using Microsoft.IdentityModel.Tokens;

namespace JWTS.Services

{

public class JWTService : IJWTService

{

private readonly TokenParameter _tokenParameter;

      public JWTService(IConfiguration configuration)

      {

        _tokenParameter = configuration.GetSection("TokenParameter").Get<TokenParameter>();

      }

     /// <summary>

/// JWT由三部分组成(Header、Payload、Signature)

/// {Header}.{Payload}.{Signature}

/// </summary>

/// <param name="userName"></param>

/// <param name="role"></param>

/// <returns></returns>

public string GetToken(string userName,string role)

{

Claim[] claims = new[]

{

new Claim(ClaimTypes.Name, userName),

new Claim("NickName","Richard"),

new Claim("Role",role)//传递其他信息

};

SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenParameter.SecurityKey));

SigningCredentials creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

/**

* Claims (Payload)

Claims 部分包含了一些跟这个 token 有关的重要信息。 JWT 标准规定了一些字段,下面节选一些字段:

JWT会被加密,但是这部分内容任何人都可以读取,所以不要存放机密信息

iss: The issuer of the token,token 是给谁的

sub: The subject of the token,token 主题

exp: Expiration Time。 token 过期时间,Unix 时间戳格式

iat: Issued At。 token 创建时间, Unix 时间戳格式

jti: JWT ID。针对当前 token 的唯一标识

除了规定的字段外,可以包含其他任何 JSON 兼容的字段。

* */

var token = new JwtSecurityToken(

issuer: _tokenParameter.Issuer,

audience: _tokenParameter.Audience,

claims: claims,

expires: DateTime.Now.AddMinutes(10),//10分钟有效期

signingCredentials: creds);

string returnToken = new JwtSecurityTokenHandler().WriteToken(token);

return returnToken;

}

}

}

6、jwt中定义好的Claims

JWT标准里面定好的claim有:

  • iss(Issuser):代表这个JWT的签发主体;
  • sub(Subject):代表这个JWT的主体,即它的所有人;
  • aud(Audience):代表这个JWT的接收对象;
  • exp(Expiration time):是一个时间戳,代表这个JWT的过期时间;
  • nbf(Not Before):是一个时间戳,代表这个JWT生效的开始时间,意味着在这个时间之前验证JWT是会失败的;
  • iat(Issued at):是一个时间戳,代表这个JWT的签发时间;
  • jti(JWT ID):是JWT的唯一标识。

7、在鉴权项目工程Startup.cs文件里依赖注入JWT的服务类

?

1
public void ConfigureServices(IServiceCollection services) { services.AddScoped <IJWTService, JWTService> (); services.AddControllers(); }

8、添加AuthenticationController,生成Token,后期可以添加RefreshToken

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52
using JWTS.Services;

using Microsoft.AspNetCore.Mvc;

using Microsoft.Extensions.Configuration;

using Microsoft.Extensions.Logging;

namespace JWTS.Controllers

{

[Route("api/[controller]")]

[ApiController]

public class AuthenticationController : ControllerBase

{

#region 构造函数

private ILogger<AuthenticationController> _logger;

private IJWTService _iJWTService;

private readonly IConfiguration _iConfiguration;

public AuthenticationController(ILogger<AuthenticationController> logger,

IConfiguration configuration

, IJWTService service)

{

_logger = logger;

_iConfiguration = configuration;

_iJWTService = service;

}

#endregion

/// <summary>

/// 实际场景使用Post方法

/// http://localhost:5000/api/Authentication/Login?name=william&password=123123

/// </summary>

/// <param name="name"></param>

/// <param name="password"></param>

/// <returns></returns>

[Route("Login")]

[HttpGet]

public IActionResult Login(string name, string password)

{

//这里应该是需要去连接数据库做数据校验,为了方便所有用户名和密码写死了

if ("william".Equals(name) && "123123".Equals(password))//应该数据库

{

var role = "Administrator";//可以从数据库获取角色

string token = this._iJWTService.GetToken(name, role);

return new JsonResult(new

{

result = true,

token

});

}

return Unauthorized("Not Register!!!");

}

}

}

2、资源中心API:使用从认证服务中心获取的Token,去访问资源,资源中心对用户信息以及Token进行鉴权操作,认证失败返回401

1、资源中心添加Nuget包(Microsoft.AspNetCore.Authentication.JwtBearer)

2、添加Authentication服务,添加JwtBearer,通过Configuration获取TokenParameter对象

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60
using System;

using System.Text;

using API.Core.Models;

using Microsoft.AspNetCore.Authentication.JwtBearer;

using Microsoft.AspNetCore.Builder;

using Microsoft.AspNetCore.Hosting;

using Microsoft.Extensions.Configuration;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Hosting;

using Microsoft.IdentityModel.Tokens;

namespace API.Core

{

public class Startup

{

private TokenParameter _tokenParameter;

public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)

{

Configuration = configuration;

_tokenParameter = configuration.GetSection("TokenParameter").Get<TokenParameter>()??throw new ArgumentNullException(nameof(_tokenParameter));

}

public void ConfigureServices(IServiceCollection services)

{

services.AddControllers();

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)//默认授权机制

.AddJwtBearer(options =>

{

options.TokenValidationParameters=new TokenValidationParameters()

{

ValidateIssuer = true,

ValidateAudience = true,

ValidateLifetime = true,

ValidateIssuerSigningKey = true,

ValidIssuer = _tokenParameter.Issuer,

ValidAudience = _tokenParameter.Audience,

IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenParameter.SecurityKey))

};

});

}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

if (env.IsDevelopment())

{

app.UseDeveloperExceptionPage();

}

app.UseRouting();

app.UseAuthentication();

app.UseAuthorization();

app.UseEndpoints(endpoints =>

{

endpoints.MapControllers();

});

}

}

}

3、在资源控制器上添加[Authorize]属性,以启用认证授权访问API资源

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30
[ApiController]

[Route("[controller]")]

[Authorize]

public class WeatherForecastController : ControllerBase

{

private static readonly string[] Summaries = new[]

{

"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"

};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)

{

_logger = logger;

}

[HttpGet]

public IEnumerable<WeatherForecast> Get()

{

var rng = new Random();

return Enumerable.Range(1, 5).Select(index => new WeatherForecast

{

Date = DateTime.Now.AddDays(index),

TemperatureC = rng.Next(-20, 55),

Summary = Summaries[rng.Next(Summaries.Length)]

})

.ToArray();

}

}

到此这篇关于ASP.NET Core使用JWT认证授权的方法的文章就介绍到这了,更多相关ASP.NET Core JWT认证授权 内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://www.cnblogs.com/william-CuiCui0705/p/13927025.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 ASP.NET Core使用JWT认证授权的方法 https://www.kuaiidc.com/97506.html

相关文章

发表评论
暂无评论