ASP.NET Core学习之使用JWT认证授权详解

2025-05-29 0 71

概述

认证授权是很多系统的基本功能 , 在以前PC的时代 , 通常是基于cookies-session这样的方式实现认证授权 , 在那个时候通常系统的用户量都不会很大, 所以这种方式也一直很好运行, 随着现在都软件用户量越来越大, 系统架构也从以前垂直扩展(增加服务器性能) -> 水平扩展(增加服务器数量)

cookies-session 工作方式

客户端提交用户信息 -> 服务器识别用户 -> 服务端保存用户信息 -> 返回session-id客户端 -> 客户端保存session-id -> 每次请求cookies带上session-id

这种方式也不是不能水平扩展 , 例如 , session复制/第三方保存session(数据库 , Redis)

名词解析

认证 : 识别用户是否合法

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

鉴权: 鉴定权限是否合法

Jwt优势与劣势

优势

无状态

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

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

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

AspNetCore集成Jwt认证

1、添加包

?

1
dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer

2、添加配置

?

1

2

3

4

5
"JwtOptions": {

"Issuer": "https://localhost:5001",

"Audience": "https://localhost:5001",

"SecurityKey": "1G3l0yYGbOINId3A*ioEi4iyxR7$SPzm"

}

3、Jwt Bearer 扩展(选项)

?

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
public static AuthenticationBuilder AddJwtBearer(this IServiceCollection services, Action<JwtOptions> configureOptions)

{

if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));

var jwtOptions = new JwtOptions()

{

Issuer = "Jwt Authentication",

Audience = "Wilson Pan Web Api",

};

// set customs optoins

configureOptions(jwtOptions);

// update Options

services.PostConfigure<JwtOptions>(options =>

{

options.Issuer = jwtOptions.Issuer;

options.Audience = jwtOptions.Audience;

options.SecurityKey = jwtOptions.SecurityKey;

});

return services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)

.AddJwtBearer(options =>

{

options.TokenValidationParameters = new TokenValidationParameters()

{

ValidIssuer = jwtOptions.Issuer,

ValidAudience = jwtOptions.Audience,

ValidateIssuer = true,

ValidateLifetime = true,

ValidateIssuerSigningKey = true,

IssuerSigningKey = jwtOptions.SymmetricSecurityKey

};

});

}

4、ConfigureServices

?

1

2

3

4

5

6
services.AddJwtBearer(options =>

{

options.Issuer = Configuration.GetValue<string>("JwtOptions:Issuer");

options.Audience = Configuration.GetValue<string>("JwtOptions:Audience");

options.SecurityKey = Configuration.GetValue<string>("JwtOptions:SecurityKey");

});

5、Configure

?

1

2
app.UseAuthentication();

app.UseAuthorization();

6、add AuthorizeController

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
//define claim

var claims = new Claim[]

{

new Claim(ClaimTypes.Name, username),

new Claim(ClaimTypes.Email, $"{username}@github.com"),

new Claim(ClaimTypes.Role, username == "WilsonPan" ? "Admin" : "Reader"),

new Claim(ClaimTypes.Hash, JwtHashHelper.GetHashString($"{username}:{password}:{System.DateTime.Now.Ticks}")),

};

//define JwtSecurityToken

var token = new JwtSecurityToken(

issuer: _jwtOptions.Issuer,

audience: _jwtOptions.Audience,

claims: claims,

expires: System.DateTime.Now.AddMinutes(5),

signingCredentials: _jwtOptions.SigningCredentials

);

// generate token

var result = new JwtSecurityTokenHandler().WriteToken(token);

7、Contrller/Action 添加认证授权

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
[ApiController]

[Authorize]

[Route("[controller]")]

public class ApiController : ControllerBase

{

...

}

[HttpPost]

[Authorize(Roles = "Admin")]

public IActionResult Post()

{

return Ok();

}

Rest Client

?

1
dotnet run

1、认证接口

?

1

2

3

4

5

6

7

8

9
@host = https://localhost:5001

# @name token

POST {{host}}/Authorize HTTP/1.1

Content-Type: application/x-www-form-urlencoded

#username=Wilson&password=123456

# admin

username=WilsonPan&password=123456

2、需要授权接口

?

1

2

3
### required authorize

GET {{host}}/api HTTP/1.1

Authorization: Bearer {{token.response.body.*}}

3、需要管理员角色接口

?

1

2

3
### required authorize

POST {{host}}/api HTTP/1.1

Authorization: Bearer {{token.response.body.*}}

示例代码

总结

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

原文链接:https://www.cnblogs.com/WilsonPan/p/13495936.html

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 ASP.NET Core学习之使用JWT认证授权详解 https://www.kuaiidc.com/97845.html

相关文章

发表评论
暂无评论