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對象。