详解ABP框架中Session功能的使用方法(2)

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>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>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>The session.</param> public static UserIdentifier ToUserIdentifier(this IAbpSession session) { return session.UserId == null ? null : new UserIdentifier(session.TenantId, session.GetUserId()); } } }

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjypfw.html