如果一个应用程序需要登录,则它必须知道当前用户执行了什么操作。因此ASP.NET在展示层提供了一套自己的SESSION会话对象,而ABP则提供了一个可以在任何地方
获取当前用户和租户的IAbpSession接口。
关于IAbpSession
需要获取会话信息则必须实现IAbpSession接口。虽然你可以用自己的方式去实现它(IAbpSession),但是它在module-zero项目中已经有了完整的实现。
注入Session
IAbpSession通常是以属性注入的方式存在于需要它的类中,不需要获取会话信息的类中则不需要它。如果我们使用属性注入方式,我们可以用
NullAbpSession.Instance作为默认值来初始化它(IAbpSession),如下所示:
public class MyClass : ITransientDependency { public IAbpSession AbpSession { get; set; } public MyClass() { AbpSession = NullAbpSession.Instance; } public void MyMethod() { var currentUserId = AbpSession.UserId; //... } }
由于授权是应用层的任务,因此我们应该在应用层和应用层的上一层使用IAbpSession(我们不在领域层使用IAbpSession是很正常的)。
ApplicationService, AbpController 和 AbpApiController 这3个基类已经注入了AbpSession属性,因此在Application Service的实例方法中,能直接使用AbpSession属性。
使用Session属性
AbpSession定义的一些关键属性:
- UserId: 当前用户的标识ID,如果没有当前用户则为null.如果需要授权访问则它不可能为空。
- TenantId: 当前租户的标识ID,如果没有当前租户则为null。
- MultiTenancySide: 可能是Host或Tenant。
UserId和TenantId是可以为null的。当然也提供了不为空时获取数据的 GetUserId()和GetTenantId() 方法 。当你确定有当前用户时,你可以使用GetUserId()方法。
如果当前用户为空,使用该方法则会抛出一个异常。GetTenantId()的使用方式和GetUserId()类似。
ABP如何实现Session的
目录代码:
类图:
IAbpSession:IAbpSession接口
using Abp.MultiTenancy; namespace Abp.Runtime.Session { public interface IAbpSession { long"htmlcode">using Abp.MultiTenancy; namespace Abp.Runtime.Session { /// <summary> /// Implements null object pattern for <see cref="IAbpSession"/>. /// </summary> public class NullAbpSession : IAbpSession { /// <summary> /// Singleton instance. /// </summary> public static NullAbpSession Instance { get { return SingletonInstance; } } private static readonly NullAbpSession SingletonInstance = new NullAbpSession(); /// <inheritdoc/> public long"htmlcode">using System; using System.Linq; using System.Security.Claims; using System.Threading; using Abp.Configuration.Startup; using Abp.MultiTenancy; using Abp.Runtime.Security; namespace Abp.Runtime.Session { /// <summary> /// Implements <see cref="IAbpSession"/> to get session properties from claims of <see cref="Thread.CurrentPrincipal"/>. /// </summary> public class ClaimsAbpSession : IAbpSession { private const int DefaultTenantId = 1; public virtual long"htmlcode">using System; namespace Abp.Runtime.Session { /// <summary> /// Extension methods for <see cref="IAbpSession"/>. /// </summary> public static class AbpSessionExtensions { /// <summary> /// Gets current User's Id. /// Throws <see cref="AbpException"/> if <see cref="IAbpSession.UserId"/> is null. /// </summary> /// <param name="session">Session object.</param> /// <returns>Current User's Id.</returns> public static long GetUserId(this IAbpSession session) { if (!session.UserId.HasValue) { throw new AbpException("Session.UserId is null! Probably, user is not logged in."); } return session.UserId.Value; } /// <summary> /// Gets current Tenant's Id. /// Throws <see cref="AbpException"/> if <see cref="IAbpSession.TenantId"/> is null. /// </summary> /// <param name="session">Session object.</param> /// <returns>Current Tenant's Id.</returns> /// <exception cref="AbpException"></exception> public static int GetTenantId(this IAbpSession session) { if (!session.TenantId.HasValue) { throw new AbpException("Session.TenantId is null! Possible problems: No user logged in or current logged in user in a host user (TenantId is always null for host users)."); } return session.TenantId.Value; } /// <summary> /// Creates <see cref="UserIdentifier"/> from given session. /// Returns null if <see cref="IAbpSession.UserId"/> is null. /// </summary> /// <param name="session">The session.</param> public static UserIdentifier ToUserIdentifier(this IAbpSession session) { return session.UserId == null ? null : new UserIdentifier(session.TenantId, session.GetUserId()); } } }明霞山资源网 Design By www.htccd.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。