AspNetCore3.1_Secutiry源码解析_4_Authentication_JwtBear

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

AspNetCore3.1_Secutiry源码解析_4_Authentication_JwtBear

holdengong   2020-03-23 我要评论
--- title: "AspNetCore3.1_Secutiry源码解析_4_Authentication_JwtBear" date: 2020-03-22T16:29:29+08:00 draft: false --- # 系列文章目录 - [AspNetCore3.1_Secutiry源码解析_1_目录](https://holdengong.com/aspnetcore3.1_secutiry源码解析_1_目录) - [AspNetCore3.1_Secutiry源码解析_2_Authentication_核心流程](https://holdengong.com/aspnetcore3.1_secutiry源码解析_2_authentication_核心流程) - [AspNetCore3.1_Secutiry源码解析_3_Authentication_Cookies](https://holdengong.com/aspnetcore3.1_secutiry源码解析_3_authentication_cookies) - [AspNetCore3.1_Secutiry源码解析_4_Authentication_JwtBear](https://holdengong.com/aspnetcore3.1_secutiry源码解析_4_authentication_jwtbear) - AspNetCore3.1_Secutiry源码解析_5_Authentication_OAuth - AspNetCore3.1_Secutiry源码解析_6_Authentication_OpenIdConnect - AspNetCore3.1_Secutiry源码解析_7_Authentication_其他 - AspNetCore3.1_Secutiry源码解析_8_Authorization_核心项目 - AspNetCore3.1_Secutiry源码解析_9_Authorization_Policy # JwtBear简介 首先回想一下Cookie认证,Cookie认证在用户登录成功之后将用户信息加密后写入浏览器Cookie中,服务端通过解析Cookie内容来验证用户登录状态。这样做有几个缺陷: - Cookie加密方式是微软自己定义的,并非国际标准,其他语言无法识别。 - 依赖Cookie,在跨域场景下,存在诸多限制。 - CORS除非设置白名单否则是不允许带Cookie的; - 大部分浏览器对跨域设置Cookie有严格的限制。比如:A网站使用iframe嵌套B网站来实现集成,B网站依赖Cookie来维持登录态,如果是Chrome浏览器,需要将Cookie的Secure设置为true,即必须使用https,同时将SameSite设置为None,这样可以解决问题但是存在跨站访问攻击(CSRF)的安全漏洞,而Safari则是完全禁止设置跨站Cookie的) JwtBear可以解决上面的缺点 - Jwt是国际标准 - Jwt不依赖Cookie,不存在跨站访问攻击问题 # 依赖注入 提供了四个重载方法,主要设置配置类 JwtBearerOptions。 默认添加名称为Bearer的认证Schema,JwtBearerHandler为处理器类。 ```csharp public static class JwtBearerExtensions { public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder) => builder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, _ => { }); public static AuthenticationBuilder AddJwtBearer(this AuthenticationBuilder builder, Action

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们