.NET core 3.0如何使用Jwt保护api详解

2025-05-29 0 97

摘要:

本文演示如何向有效用户提供jwt,以及如何在webapi中使用该token通过JwtBearerMiddleware中间件对用户进行身份认证。

认证和授权区别?

首先我们要弄清楚认证(Authentication)和授权(Authorization)的区别,以免混淆了。认证是确认的过程中你是谁,而授权围绕是你被允许做什么,即权限。显然,在确认允许用户做什么之前,你需要知道他们是谁,因此,在需要授权时,还必须以某种方式对用户进行身份验证。

什么是JWT

根据维基百科的定义,JSON WEB Token(JWT),是一种基于JSON的、用于在网络上声明某种主张的令牌(token)。JWT通常由三部分组成:头信息(header),消息体(payload)和签名(signature)。

头信息指定了该JWT使用的签名算法:

?

1
header = '{"alg":"HS256","typ":"JWT"}'

HS256表示使用了HMAC-SHA256来生成签名。

消息体包含了JWT的意图:

?

1
payload = '{"loggedInAs":"admin","iat":1422779638}'//iat表示令牌生成的时间

未签名的令牌由base64url编码的头信息和消息体拼接而成(使用"."分隔),签名则通过私有的key计算而成:

?

1

2

3
key = 'secretkey'

unsignedToken = encodeBase64(header) + '.' + encodeBase64(payload)

signature = HMAC-SHA256(key, unsignedToken)

最后在未签名的令牌尾部拼接上base64url编码的签名(同样使用"."分隔)就是JWT了:

?

1

2

3
token = encodeBase64(header) + '.' + encodeBase64(payload) + '.' + encodeBase64(signature)

# token看起来像这样: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI

JWT常常被用作保护服务端的资源(resource),客户端通常将JWT通过HTTP的Authorization header发送给服务端,服务端使用自己保存的key计算、验证签名以判断该JWT是否可信:

?

1
Authorization: Bearer eyJhbGci*...<snip>...*yu5CSpyHI

准备工作

使用vs2019创建webapi项目,并且安装nuget包

?

1
Microsoft.AspNetCore.Authentication.JwtBearer

.NET core 3.0如何使用Jwt保护api详解

Startup类

ConfigureServices 添加认证服务

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
services.AddAuthentication(options =>

{

options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;

options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;

}).AddJwtBearer(options =>

{

options.SaveToken = true;

options.RequireHttpsMetadata = false;

options.TokenValidationParameters = new TokenValidationParameters()

{

ValidateIssuer = true,

ValidateAudience = true,

ValidAudience = "https://www.cnblogs.com/chengtian",

ValidIssuer = "https://www.cnblogs.com/chengtian",

IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SecureKeySecureKeySecureKeySecureKeySecureKeySecureKey"))

};

});

Configure 配置认证中间件

?

1
app.UseAuthentication();//认证中间件、

创建一个token

添加一个登录model命名为LoginInput

?

1

2

3

4

5

6

7
public class LoginInput

{

public string Username { get; set; }

public string Password { get; set; }

}

添加一个认证控制器命名为AuthenticateController

?

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
[Route("api/[controller]")]

public class AuthenticateController : Controller

{

[HttpPost]

[Route("login")]

public IActionResult Login([FromBody]LoginInput input)

{

//从数据库验证用户名,密码

//验证通过 否则 返回Unauthorized

//创建claim

var authClaims = new[] {

new Claim(JwtRegisteredClaimNames.Sub,input.Username),

new Claim(JwtRegisteredClaimNames.Jti,Guid.NewGuid().ToString())

};

IdentityModelEventSource.ShowPII = true;

//签名秘钥 可以放到json文件中

var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SecureKeySecureKeySecureKeySecureKeySecureKeySecureKey"));

var token = new JwtSecurityToken(

issuer: "https://www.cnblogs.com/chengtian",

audience: "https://www.cnblogs.com/chengtian",

expires: DateTime.Now.AddHours(2),

claims: authClaims,

signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)

);

//返回token和过期时间

return Ok(new

{

token = new JwtSecurityTokenHandler().WriteToken(token),

expiration = token.ValidTo

});

}

}

添加api资源

利用默认的控制器WeatherForecastController

  • 添加个Authorize标签
  • 路由调整为:[Route("api/[controller]")] 代码如下
?

1

2

3

4
[Authorize]

[ApiController]

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

public class WeatherForecastController : ControllerBase

到此所有的代码都已经准好了,下面进行运行测试

运行项目

使用postman进行模拟

输入url:https://localhost:44364/api/weatherforecast

.NET core 3.0如何使用Jwt保护api详解

发现返回时401未认证,下面获取token

通过用户和密码获取token

.NET core 3.0如何使用Jwt保护api详解

如果我们的凭证正确,将会返回一个token和过期日期,然后利用该令牌进行访问

利用token进行请求

.NET core 3.0如何使用Jwt保护api详解

ok,最后发现请求状态200!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。

原文链接:https://www.cnblogs.com/chengtian/p/11927663.html

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 .NET core 3.0如何使用Jwt保护api详解 https://www.kuaiidc.com/98441.html

相关文章

发表评论
暂无评论