博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET core3.1使用cookie进行身份认证
阅读量:4034 次
发布时间:2019-05-24

本文共 4409 字,大约阅读时间需要 14 分钟。

一个系统,用户身份认证少不了,ASP.NET Core提供完整的解决方案Identity,用户创建和维护登录名;也提供能cookie和JwtBearer认证方案,当然你可以使用第三方认证Oauth、openId。项目没有采用前后端分离,是一个标准的mvc项目,所以本文采用系统提供的cookie认证 记录一下简单认证流程,(1)使用用户账号密码进行登录,验证合法登录(2)确认合法身份之后,会颁发一个认证票据(加密)会携带与该用户相关的身份、权限以及其他信息。(3)退出。

主要会使用Microsoft.AspNetCore.Authentication.Abstractions包中 AuthenticationHttpContextExtensions类,它是基于HttpContext上公开身认证的扩展法:

方法 描述
SignInAsync 登录用户.用户登录成功后颁发一个证书(加密的用户凭证,这个凭证放入Cookie中),用来标识用户的身份
SignOutAsync 注销退出.清除Cookie
GetTokenAsync 用来获取 AuthenticationProperties 中保存的额外信息
ForbidAsync 通知用户权限不足,如果是ajax请求返回403状态码,不是ajax请求跳转指定的url
ChallengeAsync 通知用户需要登录。在默认实现类AuthenticationHandler中,返回401
AuthenticateAsync 验证在 SignInAsync 中颁发的证书,并返回一个 AuthenticateResult 对象,表示用户的身份。

登录创建一个cookie认证

这里涉及几个对象:Claim声明常常代表,认证用户身份的元数据信息,比如手机号、邮箱、用户名等等。ClaimsIdentity声明主体,代表一个认证用户的身份证,当然包含声明的集合。ClaimsPrincipal身份证持有者。

流程:创建一个包含用户信息的 cookie需要构造一个ClaimsPrincipal。将序列化用户信息并将其存储在中 cookie 。

用必要的 Claim来构造一个ClaimsIdentity,然后调用 SignInAsync 来登录用户。

public async Task
UserLogin([FromForm] UserLoginInput input) { //登录逻辑 var model = userService.UserLogin(input); //1.创建cookie 保存用户信息,使用claim。将序列化用户信息并将其存储在cookie中 var claims = new List
() { new Claim(ClaimTypes.MobilePhone,model.Mobile), new Claim(ClaimTypes.Name,model.UserName), new Claim("Id",model.SysNo.ToString()) }; //2.创建声明主题 指定认证方式 这里使用cookie var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); //3.配置认证属性 比如过期时间,是否持久化。。。。 var authProperties = new AuthenticationProperties { //AllowRefresh =
, // Refreshing the authentication session should be allowed. //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10), // The time at which the authentication ticket expires. A // value set here overrides the ExpireTimeSpan option of // CookieAuthenticationOptions set with AddCookie. //IsPersistent = true, //持久化 ,比如 登录的时候 勾选记住我 复选框 //IssuedUtc =
, //绝对cookie过期 //RedirectUri =
// The full path or absolute URI to be used as an http // redirect response value. }; //4.登录 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); return Result.ResponseSuccess(); }

SignInAsync 创建一个加密的 cookie ,并将其添加到当前响应中。

退出

退出很简单,主要用户清除cookie

HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

获取认证信息

登录之后,我们通常会获取一些声明中的信息,可以使用 HttpContext.User 将会返回一个ClaimsPrincipal对象

ClaimsPrincipal principal = HttpContext.User;            if (null != principal)            {                foreach (Claim claim in principal.Claims)                {                    var ii = "CLAIM TYPE: " + claim.Type + "; CLAIM VALUE: " + claim.Value + "
"; } }

统一处理获取到的信息,赋值UserViewModel实例CurrentLoginUser

public class BaseController : Controller    {        public UserViewModel CurrentLoginUser        {            get            {                var principal = HttpContext.User;                if (principal != null)                {                    return new UserViewModel()                    {                        UserName = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value,                        Mobile = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.MobilePhone)?.Value,                        SysNo = new Guid(principal.Claims.FirstOrDefault(x => x.Type == "Id")?.Value ?? Guid.Empty.ToString())                    };                }                return null;            }        }

使用

var userId = CurrentLoginUser.SysNo;

startup类配置

在ConfigureServices方法添加

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)                .AddCookie(options =>                {                    options.LoginPath =new PathString("/User/Login");                });

在Configure方法添加

application.UseAuthentication();            application.UseAuthorization();

参考:

https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/cookie?view=aspnetcore-3.1

转载地址:http://xhzdi.baihongyu.com/

你可能感兴趣的文章
c++字符数组和字符指针区别以及str***函数
查看>>
c++类的操作符重载注意事项
查看>>
c++模板与泛型编程
查看>>
STL::deque以及由其实现的queue和stack
查看>>
WAV文件解析
查看>>
DAC输出音乐2-解决pu pu 声
查看>>
WPF中PATH使用AI导出SVG的方法
查看>>
WPF UI&控件免费开源库
查看>>
QT打开项目提示no valid settings file could be found
查看>>
Win10+VS+ESP32环境搭建
查看>>
Ubuntu+win10远程桌面
查看>>
flutter-实现圆角带边框的view(android无效)
查看>>
flutter-实现一个下拉刷新上拉加载的列表
查看>>
android 代码实现圆角
查看>>
flutter-解析json
查看>>
android中shader的使用
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
JavaScript基础知识(2)
查看>>