星空网 > 软件开发 > ASP.net

[Architect] ABP(现代ASP.NET样板开发框架) Session

本节目录

  • 介绍
    • 关于IAbpSession
  • session注入
  • 使用session属性

介绍

如果应用需要登录,则需要知道当前用户正在执行操作.在Asp.Net中,已经提供了Session对象在展现层.Abp提供IAbpSession 接口,在任何需要的地方来获得当前用户和租户.

 

关于IAbpSession

IAbpSession 需要实现获取真实的session信息.虽然你可以用自己的方式实现,在module-zero项目中已经完整的实现了.


IAbpSession 完全集成到Abp中.(设置系统和授权系统)

 

session注入

IAbpSession 在使用的类中,通常采用属性注入.除非没有session信息导致不能工作.如果我们使用属性注入,我们可以使用NullAbpSession.Instance 作为默认值

public class MyClass : ITransientDependency{  public IAbpSession AbpSession { get; set; }  public MyClass()  {    AbpSession = NullAbpSession.Instance;  }  public void MyMethod()  {    var currentUserId = AbpSession.UserId;    //...  }}

因为授权是应用层的任务.一般在应用层或更高层使用IAbpSession .ApplicationServiceAbpController and AbpApiController已经继承了AbpSession.所以你可以在application service method使用AbpSession.

public class MyClass : ITransientDependency{  public IAbpSession AbpSession { get; set; }  public MyClass()  {    AbpSession = NullAbpSession.Instance;  }  public void MyMethod()  {    var currentUserId = AbpSession.UserId;    //...  }}

 

使用session 属性

AbpSession定义了一些特性.

AbpSession defines a few key properties:

  • UserId: Id of the current user or null if there is no current user. It can not be null if the calling code is authorized.
  • UserId: 当前用户的Id,为空,当前无用户.
  • TenantId: 当前租户的Id,为空,当前无租户.
  • ImpersonatorUserId: 如果当前session模拟其他用户.则为模拟用户Id.如果没有模拟登录,则为null.
  • ImpersonatorTenantId: 如果当前session模拟其他用户,则为模拟用户的租户.如果没有模拟登录,则为null.
  • MultiTenancySide: 要么是Host要么是Tenant

UserId and TenantId 可为空.GetUserId() and GetTenantId()方法返回值不为空.如果你确定当前用户存在,你可以调用GetUserId().如果当前用户不存在,调用会抛出异常.GetTenantId()同样如此.

模拟属性通常用于审计日志的目的.

 





原标题:[Architect] ABP(现代ASP.NET样板开发框架) Session

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

店管易:https://www.goluckyvip.com/tag/30473.html
店匠Shoplazza:https://www.goluckyvip.com/tag/30474.html
店匠独立站:https://www.goluckyvip.com/tag/30475.html
店铺安全:https://www.goluckyvip.com/tag/30476.html
店铺代运营:https://www.goluckyvip.com/tag/30477.html
店铺代运营方案:https://www.goluckyvip.com/tag/30478.html
黄果树瀑布景区景点 - 黄果树瀑布景区景点分布图:https://www.vstour.cn/a/408258.html
延边酒店(附近旅馆住宿50元):https://www.vstour.cn/a/409226.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流