做高端企业网站建设公司,做设计的网站商家入驻,怎样做网站表白,网站模块是指什么地方一个系统#xff0c;用户身份认证少不了#xff0c;ASP.NET Core提供完整的解决方案Identity#xff0c;用户创建和维护登录名#xff1b;也提供能cookie和JwtBearer认证方案#xff0c;当然你可以使用第三方认证Oauth、openId。项目没有采用前后端分离#xff0c;是一个… 一个系统用户身份认证少不了ASP.NET Core提供完整的解决方案Identity用户创建和维护登录名也提供能cookie和JwtBearer认证方案当然你可以使用第三方认证Oauth、openId。项目没有采用前后端分离是一个标准的mvc项目所以本文采用系统提供的cookie认证 记录一下简单认证流程1使用用户账号密码进行登录验证合法登录2确认合法身份之后会颁发一个认证票据加密会携带与该用户相关的身份、权限以及其他信息。3退出。主要会使用Microsoft.AspNetCore.Authentication.Abstractions包中 AuthenticationHttpContextExtensions类它是基于HttpContext上公开身认证的扩展法方法描述SignInAsync登录用户.用户登录成功后颁发一个证书加密的用户凭证这个凭证放入Cookie中用来标识用户的身份SignOutAsync注销退出.清除CookieGetTokenAsync用来获取 AuthenticationProperties 中保存的额外信息ForbidAsync通知用户权限不足如果是ajax请求返回403状态码不是ajax请求跳转指定的urlChallengeAsync通知用户需要登录。在默认实现类AuthenticationHandler中返回401AuthenticateAsync验证在 SignInAsync 中颁发的证书并返回一个 AuthenticateResult 对象表示用户的身份。登录创建一个cookie认证这里涉及几个对象Claim声明常常代表认证用户身份的元数据信息比如手机号、邮箱、用户名等等。ClaimsIdentity声明主体代表一个认证用户的身份证当然包含声明的集合。ClaimsPrincipal身份证持有者。流程创建一个包含用户信息的 cookie需要构造一个ClaimsPrincipal。将序列化用户信息并将其存储在中 cookie 。用必要的 Claim来构造一个ClaimsIdentity然后调用 SignInAsync 来登录用户。 public async TaskResult UserLogin([FromForm] UserLoginInput input){//登录逻辑var model userService.UserLogin(input);//1.创建cookie 保存用户信息使用claim。将序列化用户信息并将其存储在cookie中var claims new ListClaim(){new Claim(ClaimTypes.MobilePhone,model.Mobile),new Claim(ClaimTypes.Name,model.UserName),new Claim(Id,model.SysNo.ToString())};//2.创建声明主题 指定认证方式 这里使用cookievar claimsIdentity new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);//3.配置认证属性 比如过期时间是否持久化。。。。var authProperties new AuthenticationProperties{//AllowRefresh bool,// 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 DateTimeOffset,//绝对cookie过期//RedirectUri string// 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 并将其添加到当前响应中。退出退出很简单主要用户清除cookieHttpContext.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 /br;}}
统一处理获取到的信息赋值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?viewaspnetcore-3.1