如果一个应用程序需要登录,则它必须知道当前用户执行了什么操作。因此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.hbtsch.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]

