.NET CORE 鉴权
基础信息
1.什么是鉴权授权?
- 鉴权是验证用户是否拥有访问系统的权利,授权是判断用户是否有权限做一些其他操作。
2.传统的Session 和Cookie
- 主要用于无状态请求下的的用户身份识别,只不过Session将信息存储在服务端,Cookie将信息存储在客户端。
Session
在客户端第一次进行访问时,服务端会生成一个Session id返回到客户端
客户端将Session id存储在本地后续每一次请求都带上这个id
服务端从接收到的请求中根据Session id在自己存储的信息中识别客户端
Cookie
在客户端访问服务器时,服务端会在响应中颁发一个Cookie
客户端会把cookie存储,当再访问服务端时会将cookie和请求一并提交
服务端会检查cookie识别客户端,并也可以根据需要修改cookie的内容
3.存在的问题
在分布式或集群系统中使用Session
假设现在服务器为了更好的承载和容灾将系统做了分布式和集群,也就是有了N个服务端,那是不是每一个服务端都要具有对每一个客户端的Session或者Cookie的识别能力呢?
这个可以使用Session共享的方式用于Session的识别,但是这并不能解决分布式系统下依然存在这个问题,因为通常每一个分布式系统都由不同的人负责或者跨网络,甚至不同的公司,不可能全部都做session共享吧?这个时候就诞生了一个新的方式,使用Token
4.Token
- Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌。
执行步骤
用户向统一的鉴权授权系统发起用户名和密码的校验
校验通过后会颁发一个Token,用户就拿着颁发的Token去访问其他三方系统
三方系统可以直接请求鉴权授权系统验证当前Token的合法性,也可以根据对称加密使用秘钥解密Token以验证合法性
.NET Core中鉴权
Authentication:鉴定身份信息,例如用户有没有登录,用户基本信息Authorization:判定用户有没有权限
1.NET Core鉴权授权基本概念
在NETCORE中鉴权授权是通过AuthenticationHttpContextExtensions扩展类中的实现的HttpContext的扩展方法来完成的
public static class AuthenticationHttpContextExtensions
{
public static Task SignInAsync(this HttpContext context, string scheme, ClaimsPrincipal principal, AuthenticationProperties properties)
{
context.RequestServices.GetRequiredService<IAuthenticationService>().SignInAsync(context, scheme, principal, properties);
}
}
它真正的核心在
Microsoft.AspNetCore.Authorization模块,整个流程处理主要包含如下几个关键类
IAuthenticationHandlerProvider
负责对用户凭证的验证,提供IAuthenticationHandler处理器给IAuthenticationService用于处理鉴权请求,当然可以自定义处理器
IAuthenticationSchemeProvider
选择标识使用的是哪种认证方式
IAuthenticationService
提供鉴权统一认证的5个核心业务接口
public interface IAuthenticationService
{
//查询鉴权
Task<AuthenticateResult> AuthenticateAsync(HttpContext context, string scheme);
//登录写入鉴权凭证
Task SignInAsync(HttpContext context, string scheme, ClaimsPrincipal principal, AuthenticationProperties properties);
//退出登录清理凭证
Task SignOutAsync(HttpContext context, string scheme, AuthenticationProperties properties);
Task ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties);
Task ForbidAsync(HttpContext context, string scheme, AuthenticationProperties properties);
}
在它的实现类AuthenticationService中的SignInAsync方法
配合IAuthenticationHandlerProvider 和IAuthenticationSchemeProvider得到一个IAuthenticationHandler,最终将鉴权写入和读取都由它完成
public virtual async Task SignInAsync(HttpContext context, string scheme, ClaimsPrincipal principal, AuthenticationProperties properties)
{
if (scheme == null)
{
//IAuthenticationSchemeProvider实例
var defaultScheme = await Schemes.GetDefaultSignInSchemeAsync();
scheme = defaultScheme?.Name;
}
//IAuthenticationHandlerProvider实例获取处理器
var handler = await Handlers.GetHandlerAsync(context, scheme);
var signInHandler = handler as IAuthenticationSignInHandler;
//各自的处理器handler
//例如使用Cookie 就会注入一个CookieAuthenticationHandler
//使用JWT 就注入一个JwtBearerHandler
await signInHandler.SignInAsync(principal, properties);
}
2.使用Cookie默认流程鉴权
- 使用中间件加入管道,用于找到鉴权HttpContext.AuthenticateAsync()
//核心源码就是AuthenticationMiddleware中间件
app.UseAuthentication();
- 注入容器,将CookieAuthenticationHandler作为处理逻辑
services.AddAuthentication(options =>
{
//CookieAuthenticationDefaults.AuthenticationScheme == "Cookies"
options.DefaultAuthenticateScheme = "Cookies";
options.DefaultSignInScheme = "Cookies";
}).AddCookie();
- 在登录时写入凭证
Claims:一项信息,例如工牌的姓名是一个Claims ,工牌号码也是一个ClaimsClaimsIdentity:一组Claims 组成的信息,就是一个用户身份信息ClaimsPrincipal:一个用户有多个身份AuthenticationTicket:用户票据,用于包裹ClaimsPrincipal
[AllowAnonymous]
public async Task<IActionResult> Login(string name, string password)
{
if(name!="Admin" && password!="000000")
{
var result = new JsonResult(new{ Result = false,Message = "登录失败"});
return result;
}
//Claims ⫋ ClaimsIdentity ⫋ ClaimsPrincipal
var claimIdentity = new ClaimsIdentity("ClaimsIdentity");
claimIdentity.AddClaim(new Claim(ClaimTypes.Name, name));
claimIdentity.AddClaim(new Claim(ClaimTypes.Address, "地址信息"));
AuthenticationProperties ap = new AuthenticationProperties();
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimIdentity);
await base.HttpContext.SignInAsync("Cookies",claimsPrincipal , ap)
return new JsonResult(new{ Result = false,Message = "登录成功"});
}
4.在其他控制器上标记[Authorize]特性,在访问接口框架会自动进行鉴权并将身份信息写入上下文
[AllowAnonymous]:匿名可访问[Authorize]:必须登录才可访问
3.自定义IAuthenticationHandler
- 实现IAuthenticationHandler, IAuthenticationSignInHandler, IAuthenticationSignOutHandler三个接口
public class CoreAuthorizationHandler : IAuthenticationHandler
,IAuthenticationSignInHandler, IAuthenticationSignOutHandler
{
public AuthenticationScheme Scheme { get; private set; }
protected HttpContext Context { get; private set; }
public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
{
Scheme = scheme;
Context = context;
return Task.CompletedTask;
}
public async Task<AuthenticateResult> AuthenticateAsync()
{
var cookie = Context.Request.Cookies["CustomCookie"];
if (string.IsNullOrEmpty(cookie))
{
return AuthenticateResult.NoResult();
}
AuthenticateResult result = AuthenticateResult
.Success(Deserialize(cookie));
return await Task.FromResult(result);
}
public Task ChallengeAsync(AuthenticationProperties properties)
{
return Task.CompletedTask;
}
public Task ForbidAsync(AuthenticationProperties properties)
{
Context.Response.StatusCode = 403;
return Task.CompletedTask;
}
public Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties)
{
var ticket = new AuthenticationTicket(user, properties, Scheme.Name);
Context.Response.Cookies.Append("CoreAuthorizationHandlerCookies", Serialize(ticket));
return Task.CompletedTask;
}
public Task SignOutAsync(AuthenticationProperties properties)
{
Context.Response.Cookies.Delete("CoreAuthorizationHandlerCookies");
return Task.CompletedTask;
}
private AuthenticationTicket Deserialize(string content)
{
byte[] byteTicket = System.Text.Encoding.Default.GetBytes(content);
return TicketSerializer.Default.Deserialize(byteTicket);
}
private string Serialize(AuthenticationTicket ticket)
{
//需要引入 Microsoft.AspNetCore.Authentication
byte[] byteTicket = TicketSerializer.Default.Serialize(ticket);
return Encoding.Default.GetString(byteTicket);
}
}
- 在容器中注册自定义的Handler
services.AddAuthenticationCore(options =>
{
options.AddScheme<CoreMvcAuthenticationHandler>("AuthenticationScheme", "AuthenticationScheme");
});
.NET CORE 鉴权的更多相关文章
- ASP.NET Core 项目简单实现身份验证及鉴权
ASP.NET Core 身份验证及鉴权 目录 项目准备 身份验证 定义基本类型和接口 编写验证处理器 实现用户身份验证 权限鉴定 思路 编写过滤器类及相关接口 实现属性注入 实现用户权限鉴定 测试 ...
- .NET Core中的鉴权授权正确方式(.NET5)
一.简介 前后端分离的站点一般都会用jwt或IdentityServer4之类的生成token的方式进行登录鉴权.这里要说的是小项目没有做前后端分离的时站点登录授权的正确方式. 一.传统的授权方式 这 ...
- .Net Core使用Ocelot网关(二) -鉴权认证
前言 上一章已经简单的介绍了ocelot的使用了,但是网关暴露的接口如果什么人都能访问的话安全性就太低啦.所以我们需要去鉴权和认证.这里我们使用identityServer4给我们的网关来鉴权认证. ...
- Mongodb 认证鉴权那点事
[TOC] 一.Mongodb 的权限管理 认识权限管理,说明主要概念及关系 与大多数数据库一样,Mongodb同样提供了一套权限管理机制. 为了体验Mongodb 的权限管理,我们找一台已经安装好的 ...
- 基于Springboot集成security、oauth2实现认证鉴权、资源管理
1.Oauth2简介 OAuth(开放授权)是一个开放标准,允许用户授权第三方移动应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方移动应用或分享他们数据的所有内容,OAu ...
- 认证鉴权与API权限控制在微服务架构中的设计与实现(四)
引言: 本文系<认证鉴权与API权限控制在微服务架构中的设计与实现>系列的完结篇,前面三篇已经将认证鉴权与API权限控制的流程和主要细节讲解完.本文比较长,对这个系列进行收尾,主要内容包括 ...
- Spring Boot整合实战Spring Security JWT权限鉴权系统
目前流行的前后端分离让Java程序员可以更加专注的做好后台业务逻辑的功能实现,提供如返回Json格式的数据接口就可以.像以前做项目的安全认证基于 session 的登录拦截,属于后端全栈式的开发的模式 ...
- SpringBoot使用token简单鉴权
本文使用SpringBoot结合Redis进行简单的token鉴权. 1.简介 刚刚换了公司,所以最近有些忙碌,所以一直没有什么产出,最近朋友问我登录相关的,所以这里先写一篇简单使用token鉴权的文 ...
- springboot oauth 鉴权之——password鉴权相当于jwt鉴权模式
近期一直在研究鉴权方面的各种案例,这几天有空,写一波总结及经验. 第一步:什么是 OAuth鉴权 OAuth2是工业标准的授权协议.OAuth2取代了在2006创建的原始OAuthTM协议所做的工作. ...
随机推荐
- 腾讯 TKE 厉害了!用 eBPF绕过 conntrack 优化K8s Service,性能提升40%
Kubernetes Service[1] 用于实现集群中业务之间的互相调用和负载均衡,目前社区的实现主要有userspace,iptables和IPVS三种模式.IPVS模式的性能最好,但依然有优化 ...
- Go Error 嵌套到底是怎么实现的?
原文链接: Go Error 嵌套到底是怎么实现的? Go Error 的设计哲学是 「Errors Are Values」. 这句话应该怎么理解呢?翻译起来挺难的.不过从源码的角度来看,好像更容易理 ...
- RISC-V CPU加电执行流程
市面上采用RISC-V架构的CPU很多,且没有如X86那样高度细节的标准,故采用说明文档详细的SiFive Freedom U540-C000芯片来做介绍(下面统一称为FU540). FU540支持多 ...
- 《剑指offer》面试题03. 数组中重复的数字
问题描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中 ...
- Javascript实现让小图片一直跟着鼠标移动
Javascript实现让小图片一直跟着鼠标移动实例 注意:图片可能加载不出来,注意更换 <!doctype html> <html> <head> <met ...
- 【解决了一个小问题】golang build中因为缓存文件损坏导致的编译错误
编译的过程中出现了一个吓人的错误: GOROOT=C:\Go #gosetup GOPATH=C:\Users\ahfuzhang\go #gosetup C:\Go\bin\go.exe mod t ...
- DEEP LEARNING WITH PYTORCH: A 60 MINUTE BLITZ | TORCH.AUTOGRAD
torch.autograd 是PyTorch的自动微分引擎,用以推动神经网络训练.在本节,你将会对autograd如何帮助神经网络训练的概念有所理解. 背景 神经网络(NNs)是在输入数据上执行的嵌 ...
- Linux下进程线程,Nignx与php-fpm的进程线程方式
1.进程与线程区别 进程是程序执行时的一个实例,即它是程序已经执行到课中程度的数据结构的汇集.从内核的观点看,进程的目的就是担当分配系统资源(CPU时间.内存等)的基本单位. 线程是进程的一个执行流, ...
- 学习AJAX必知必会(1)~Ajax
一.ajax(Asynchronous JavaScript And XML,即异步的 JS 和 XML) 1.通过 AJAX 可以在浏览器中向服务器发送异步请求实现无刷新获取数据. 2.优势:无刷新 ...
- golang中循环或递归求阶乘
package main import "fmt" func factorialFor(num int) (ret int) { // 循环求阶乘 ret = 1 for i := ...