Introduction

ASP.NET Boilerplate provides IAbpSession interface to obtain current user and tenant without using ASP.NET's Session. IAbpSession is also fully integrated and used by other structures in ASP.NET Boilerplate (setting system and authorization system for instance).

ASP.NET样板提供iabpsession接口获取当前用户和租户不使用ASP。NET的会话。iabpsession也完全集成的ASP.NET样板其他结构(设置系统和授权系统为例)。

Injecting Session(会话注入)

IAbpSession is generally property injected to needed classes unless it's not possible to work without session informations. If we use property injection, we can use NullAbpSession.Instance as default value as shown below:

iabpsession一般属性注入所需的类除非没有会话信息的工作是不可能的。如果我们使用属性注入,我们可以用nullabpsession。例如默认值,如下图所示:

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

Since authentication/authorization is an application layer task, it's adviced to use IAbpSession in application layer and upper layers (we don't use it in domain layer normally). ApplicationService, AbpController,AbpApiController and some other base classes has AbpSession already injected. So, you can directly use AbpSession property in an application service method for instance.

因为认证/授权是一个应用层的任务,这是建议使用iabpsession在应用层和上层(我们不使用它在域层通常)。applicationservice,abpcontroller abpapicontroller and some other基础类,有abpsession已经注入。所以,你可以直接使用abpsession property 在应用服务的方法的实例中。

Session Properties(会话属性)

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.
  • 如果没有当前用户,则当前用户ID或NULL。如果调用代码被授权,它不能为空。
  • TenantId: Id of the current tenant or null if there is no current tenant (in case of user has not logged in or he is a host user).
  • 如果没有当前租户(在用户未登录或他是主机用户的情况下),则当前租户的ID或NULL。
  • ImpersonatorUserId: Id of the impersonator user if current session is impersonated by another user. It's null if this is not an impersonated login.
  • 身份证模拟用户如果当前会话是由模拟另一个用户。如果这不是一个模拟登录它的空。
  • ImpersonatorTenantId: Id of the impersonator user's tenant, if current session is impersonated by another user. It's null if this is not an impersonated login.
  • 身份证模拟用户的租户,如果当前会话是由模拟另一个用户。如果这不是一个模拟登录它的空。
  • MultiTenancySide: It may be Host or Tenant.
  • 可能是主机或租户。

UserId and TenantId is nullable. There is also non-nullable GetUserId() and GetTenantId() methods. If you're sure there is a current user, you can call GetUserId(). If current user is null, this method throws exception. GetTenantId() is also similar.

Impersonator properties are not common as other properties and generally used for audit logging purposes.

UserId 和TenantId可以是空。也有非空getuserid()和gettenantid()方法。
如果你确信有一个当前用户,你可以调用getuserid()。
如果当前用户为null,则此方法引发异常。gettenantid()也类似。

模拟的属性是不常见的其他属性,一般用于日志审计的目的。

ClaimsAbpSession

ClaimsAbpSession is the default implementation of IAbpSession interface. It gets session properties (except MultiTenancySide, it's calculated) from claims of current user's princical. For a cookie based form authentication, it gets from cookies. Thus, it' well integrated to ASP.NET's authentication mechanism.

claimsabpsession是iabpsession接口的默认实现。它获取会话属性(除multitenancyside,它计算)从当前的主要用户。对于基于cookie的表单身份验证,它来自cookie。因此,它很好地集成到ASP.NET的身份验证机制中。

Overriding Current Session Values(重写当前会话值)

In some specific cases, you may need to change/override session values for a limited scope. In such cases, you can use IAbpSession.Use method as shown below:

在某些特定的情况下,您可能需要更改/覆盖有限范围的会话值。在这种情况下,你可以使用iabpsession。使用方法如下图所示:

public class MyService
{
private readonly IAbpSession _session; public MyService(IAbpSession session)
{
_session = session;
} public void Test()
{
using (_session.Use(42, null))
{
var tenantId = _session.TenantId; //42
var userId = _session.UserId; //null
}
}
}

Use method returns an IDisposable and it must be disposed. Once the return value is disposed, Session values are automatically restored the to previous values.

使用方法返回一个IDisposable,必须设置。一旦返回值被处理,会话值将自动恢复到以前的值。

Warning!

Always use it in a using block as shown above. Otherwise, you may get unexpected session values. You can have nested Use blocks and they will work as you expect.

始终在使用块中使用它,如上所示。否则,您可能会获得意外的会话值。可以使用嵌套的块,它们将按照您的预期工作。

User Identifier(用户标示符)

You can use .ToUserIdentifier() extension method to create a UserIdentifier object from IAbpSession. Since UserIdentifier is used in most API, this will simplify to create a UserIdentifier object for the current user.

你可以使用。touseridentifier()扩展方法来创建一个对象从iabpsession useridentifier。因为useridentifier用的最多的就是API,这将简化创建当前用户useridentifier对象。

ABP框架系列之九:(Abp-Session-会话)的更多相关文章

  1. 老周的ABP框架系列教程 -》 一、框架理论初步学习

    老周的ABP框架系列教程 -- 一.框架理论初步学习   1. ABP框架的来源与作用简介 1.1  简介 1.1.1       ABP框架全称为"ASP.NET Boilerplate ...

  2. 2019 年起如何开始学习 ABP 框架系列文章-开篇有益

    2019 年起如何开始学习 ABP 框架系列文章-开篇有益 [[TOC]] 本系列文章推荐阅读地址为:52ABP 开发文档 https://www.52abp.com/Wiki/52abp/lates ...

  3. ABP框架系列之三十四:(Multi-Tenancy-多租户)

    What Is Multi Tenancy? "Software Multitenancy refers to a software architecture in which a sing ...

  4. ABP入门系列目录——学习Abp框架之实操演练

    ABP是"ASP.NET Boilerplate Project (ASP.NET样板项目)"的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WE ...

  5. ABP框架系列之十一:(AspNet-Core-ASPNET核心)

    Introduction This document describes ASP.NET Core integration for ASP.NET Boilerplate framework. ASP ...

  6. ABP框架系列之五十四:(XSRF-CSRF-Protection-跨站请求伪造保护)

    Introduction "Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a maliciou ...

  7. ABP框架系列之四十:(Notification-System-通知系统)

    Introduction Notifications are used to inform users on specific events in the system. ASP.NET Boiler ...

  8. ABP框架系列之四十七:(SignalR-Integration-SignalR-集成)

    Introduction Abp.Web.SignalR nuget package makes it easily to use SignalR in ASP.NET Boilerplate bas ...

  9. ABP框架系列之五十一:(Timing-定时)

    Introduction While some applications target a single timezone, some others target to many different ...

随机推荐

  1. Core Graphices 获取上下文

    Core Graphices 获取上下文的三种方式: 1.自定义view 重写view 的 drawRect:(CGRect)rect方法 - (void)drawRect:(CGRect)rect ...

  2. 关于ARM Linux下的SD卡及U盘的挂载问题

    内核配置并运行后,挂载SD卡,出现问题: zynq> mount -t /dev/mmcblk1 /mntmount: mounting /dev/mmcblk0 on /mnt failed: ...

  3. 成为JavaGC专家—深入浅出Java垃圾回收机制

    对于Java开发人员来说,了解垃圾回收机制(GC)有哪些好处呢? 首先可以满足作为一名软件工程师的求知欲, 其次,深入了解GC如何工作可以帮你写出更好的Java应用. 这仅仅代表我个人的意见,但我坚信 ...

  4. Java并发编程三个性质:原子性、可见性、有序性

      并发编程 并发程序要正确地执行,必须要保证其具备原子性.可见性以及有序性:只要有一个没有被保证,就有可能会导致程序运行不正确  线程不安全在编译.测试甚至上线使用时,并不一定能发现,因为受到当时的 ...

  5. 26.python常用端口号

    MySQL默认端口 3306 Redis默认端口 6379 MongoDB默认端口 27017 django端口 8000 flask端口 5000 pyspider服务端口 5000(由flask开 ...

  6. (9/24) 图片跳坑大战--css分离与图片路径处理

    前言: 在上一节当中,我们把小图片打包成Base64格式(打包到了js当中).我们也算是对webpack对图片的打包有个基本了解. 本节我们准备把css从JavasScript代码中分离出来,这会遇到 ...

  7. React琐碎

    1 dangerouslySetInnerHTML 使用此属性的原因是得到一个安全的数据,生成一个仅包含唯一key——__html的对象,表明这段内容会被当成text/taint使用,它的使用方式是这 ...

  8. 远程桌面 把explorer关掉了

    用Ctrl+Alt+End调出远程桌面的任务管理器.然后,运行explorer.exe即可重启该服务.

  9. idhttp.post方式 调用datasnap rest 远程方法(转咏南兄)

    idhttp.get方式调用,这种比较简单,大家都会.post方式网上却没有任何成功的代码,本人也是摸索了一个上午才搞定. 分享给大家. (1)post方式调用的远程方法,方法名必须加“update” ...

  10. PAT甲级

    https://www.cnblogs.com/jlyg/p/7525244.html 哇咔咔,瞧人家都刷完甲级130道题了,哼,有什么了不起的,考前刷完,再刷的比你多,哼,照样吊打. 沙耶加油! 一 ...