当前位置:网站首页>.NET 7 的 JWT 配置太方便了!
.NET 7 的 JWT 配置太方便了!
2022-06-30 03:34:00 【dotNET跨平台】
微软宣布 .NET 7 preview5 有一些较大的改进, 包括 JWT 身份验证的简化和自动配置。
我安装了 preview 5 尝试了新的 JWT 身份配置。如果您想把现有的项目更新到 .Net 7 preview 5, 下面是一个快速更新的命令。
Update all Microsoft.AspNetCore.* package references to 7.0.0-preview.5.*.
Update all Microsoft.Extensions.* package references to 7.0.0-preview.5.*..NET 7 之前的 JWT 认证配置
微软表示他们已经收到反馈,在 ASP.NET Core 项目中配置 JWT, 是编写 API 是最难的部分之一。它需要很多步骤,包括在启动过程中添加中间件和配置服务。
.Net 团队知道 JWT 配置在保护 API 方面的重要性,这就是为什么他们改进和简化了在 ASP.NET Core 项目中配置 JWT 身份验证的过程。
简化的 JWT 配置
WebApplicationBuilder 添加了一个新的 Authentication 属性,然后可以直接调用 AddJwtBearer() 方法,如下
var builder = WebApplication.CreateBuilder(args);
builder.Authentication.AddJwtBearer();
var app = builder.Build();使用这个新属性设置 JWT 身份验证会自动添加所需的中间件,不需要其他额外的代码。
最重要的是,现在可以直接在 appsettings.json 配置 JWT,你可以非常方便地配置多个环境。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Authentication": {
"DefaultScheme" : "JwtBearer",
"Schemes": {
"JwtBearer": {
"Audiences": [ "http://localhost:5000",
"https://localhost:5001" ],
"ClaimsIssuer": "user-jwt-here"
}
}
}
}上面就是 .NET 7 preview 5 中简化的 JWT 配置,这很方便,这种改进可以真正的帮助开发人员节省配置项目的时间。
END
做了一个 .NET 的学习网站,内容涵盖了分布式系统,数据结构与算法,设计模式,操作系统,计算机网络等,以及工作推荐和面试经验分享,欢迎来撩。

边栏推荐
- Feign pit
- How do college students make money by programming| My way to make money in College
- 124 articles in total! Motianlun "high availability architecture" dry goods document sharing (including Oracle, mysql, PG)
- Redis high concurrency distributed locks (learning summary)
- December2020 - true questions and analysis of C language (Level 2) in the youth level examination of the Electronic Society
- QT中foreach的使用
- Use of custom MVC
- Global and Chinese market of ULTRACENTRIFUGES 2022-2028: Research Report on technology, participants, trends, market size and share
- From 2500 a month, no one wants to go to the programming road of the technical director of a large factory | my ten years
- 【个人总结】学习计划
猜你喜欢

【筆記】AB測試和方差分析
![[punch in - Blue Bridge Cup] day 3 --- slice in reverse order list[: -1]](/img/c2/13693dcb51aab565957b6c5e686b7c.jpg)
[punch in - Blue Bridge Cup] day 3 --- slice in reverse order list[: -1]

Redis高并发分布式锁(学习总结)

Principle, advantages and disadvantages of three operating modes of dc/dc converter under light load

Practical debugging skills

专升本高数(四)

Redis中的SDS理解

Utilisation de foreach en Qt

hudi记录

UML diagrams and list collections
随机推荐
Neo4j--- performance optimization
laravel9本地安装
专升本高数(四)
[frequently asked questions] modularization of browser environment and node environment
General paging (2)
1152_ Makefile learning_ Pattern matching rules
LitJson解析 生成json文件 读取json文件中的字典
Global and Chinese market of bulk acoustic wave devices 2022-2028: Research Report on technology, participants, trends, market size and share
Redis在windows系统中使用
Some common functions and precautions
Note: load the configuration file as a byte stream and get the resources in it
51 single chip microcomputer indoor environment monitoring system, mq-2 smoke sensor and DHT11 temperature and humidity sensor, schematic diagram, C programming and simulation
Reasons for MySQL master-slave database synchronization failure
ReSharper 7. Can X be used with vs2013 preview? [off] - can resharper 7 x be used with VS2013 preview? [closed]
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
Half a year after joining the company, I was promoted to a management post
December2020 - true questions and analysis of C language (Level 2) in the youth level examination of the Electronic Society
MySQL performance optimization (6): read write separation
[0x0] 校长留的开放问题作业
云原生入门+容器概念介绍