当前位置: 首页 > news >正文

ShenNiusModularity项目源码学习(4:身份认证)

  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.AddScoped<IShenNiusContext, ShenNiusContext>();

参考文献:
[1]https://gitee.com/shenniu_code_group/shen-nius.-modularity

http://www.lryc.cn/news/492788.html

相关文章:

  • python+django自动化部署日志采用‌WebSocket前端实时展示
  • flink学习(6)——自定义source和kafka
  • 开发常见问题及解决
  • python excel接口自动化测试框架!
  • mybatis:You have an error in your SQL syntax;
  • 使用 Maven 开发 IntelliJ IDEA 插件
  • Windows修复SSL/TLS协议信息泄露漏洞(CVE-2016-2183) --亲测
  • uniapp生命周期:应用生命周期和页面生命周期
  • 基于SSM的婴幼儿用品商城系统+LW示例参考
  • 【工具变量】城市供应链创新试点数据(2007-2023年)
  • 【carla生成车辆时遇到的问题】carla显示的坐标和carlaworld中提取的坐标y值相反
  • Jira使用笔记二 ScriptRunner 验证问题创建角色
  • Java线程的使用
  • 自动化测试工具Ranorex Studio(四十三)-RANOREXPATH编辑器5
  • 超高流量多级缓存架构设计!
  • 数据结构(Java)—— ArrayList
  • 实习冲刺第三十三天
  • Uniapp开发下拉刷新功能onPullDownRefresh/onReachBottom
  • 什么是 C++ 中的函数对象?函数对象与普通函数有什么区别?如何定义和使用函数对象?
  • PointNet++论文复现
  • 【VUE】el-table表格内输入框或者其他控件规则校验实现
  • django开发中html继承模板样式
  • MT6769/MTK6769核心板规格参数_联发科安卓主板开发板方案
  • 鸿蒙进阶篇-状态管理之@Provide与@Consume
  • java集合及源码
  • GraphRAG访问模式和知识图谱建模
  • TCP/IP协议攻击与防范
  • Java基于 SpringBoot+Vue的口腔管理平台(附源码+lw+部署)
  • 11.26深度学习_神经网络-数据处理
  • 【人工智能】Python常用库-TensorFlow常用方法教程