cnzz站长统计怎么添加到博客网站,深圳市宝安区地图全图高清版,装修平台自己做网站有几个,网站模板修改教程ShenNiusModularity项目有两套启动方式#xff0c;一种是ShenNius.Admin.Mvc项目启动#xff0c;该项目为MVC模式#xff0c;带前台页面#xff0c;也有后台服务#xff0c;另一种是ShenNius.Admin.Hosting#xff0c;该项目启动后仅提供后台服务#xff0c;供其它前台项… ShenNiusModularity项目有两套启动方式一种是ShenNius.Admin.Mvc项目启动该项目为MVC模式带前台页面也有后台服务另一种是ShenNius.Admin.Hosting该项目启动后仅提供后台服务供其它前台项目调用。本文学习并分析ShenNius.Admin.Mvc项目中的身份认证方式。 ShenNiusModularity项目启动时在ShenNius.Admin.Mvc项目的Program文件内调用ShenniusAdminMvcModule其内部又依赖ShenNius.Admin.API项目的ShenniusAdminApiModule类在该类中启动时根据启动项目中的JwtSetting设置判断是否启动jwt身份认证。ShenNius.Admin.Mvc项目的appsettings.json没有相关jwt设置因此采用基于Cookie的身份认证方式主要代码如下所示 context.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o {o.Cookie.Name ShenNius.Admin.Mvc;o.LoginPath new PathString(/sys/user/login);o.LogoutPath new PathString(/sys/user/Logout);o.Cookie.HttpOnly true;});在Admin.Areas.Sys.Controllers.UserController的Login函数内当通过用户验证后会将用户名称、上次登录时间、电话、邮箱、是否管理员等信息保存为ClaimsPrincipal对象实例内并调用HttpContext.SignInAsync函数将信息保存到Cookie内。
var identity new ClaimsPrincipal(new ClaimsIdentity(new[]{new Claim(JwtRegisteredClaimNames.Sid,result.Id.ToString()),new Claim(ClaimTypes.Name,result.LoginName),new Claim(ClaimTypes.WindowsAccountName,result.LoginName),new Claim(ClaimTypes.UserData,result.LastLoginTime.ToString()),new Claim(ClaimTypes.MobilePhone,result.Mobile),new Claim(ClaimTypes.Email,loginModel.Email),new Claim(TrueName,result.TrueName),new Claim(TenantId,result.TenantId.ToString()),new Claim(IsAdmin,isAdmin.ToString())}, CookieAuthenticationDefaults.AuthenticationScheme));
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, identity, new AuthenticationProperties
{ExpiresUtc DateTime.UtcNow.AddHours(24),IsPersistent true,AllowRefresh false
});项目采用接口IShenNiusContext及其实现类ShenNiusContext获取当前登录用户信息其内部实现也是从HttpContext.User属性中获取用户名等信息在此不再赘述有兴趣的可以查看ShenNius.Infrastructure.ShenNiusContext源码。该接口在项目启动时已注册服务。
context.Services.AddScopedIShenNiusContext, ShenNiusContext();参考文献 [1]https://gitee.com/shenniu_code_group/shen-nius.-modularity