无为清净楼资源网 Design By www.qnjia.com

如果一个应用程序需要登录,则它必须知道当前用户执行了什么操作。因此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的
目录代码:

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

类图:

详解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());
    }
  }
}

标签:
ABP,Session,ASP.NET

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?