2017-08-21 155 views
0

我需要扩展ClaimsAbpSession并创建新的属性,以便在角度应用程序和服务器之间的请求中发送。Extend ClaimsAbpSession

实际上,我已经使用声明来存储这些新属性。但是,当用户刷新页面时,索赔中的值将丢失。

回答

0

MyAppSession.cs

//Define your own session and add your custom field to it 
//Then, you can inject MyAppSession and use it's new property in your project. 
public class MyAppSession : ClaimsAbpSession, ITransientDependency 
{ 
    public MyAppSession(
     IPrincipalAccessor principalAccessor, 
     IMultiTenancyConfig multiTenancy, 
     ITenantResolver tenantResolver, 
     IAmbientScopeProvider<SessionOverride> sessionOverrideScopeProvider) : 
     base(principalAccessor, multiTenancy, tenantResolver, sessionOverrideScopeProvider) 
    { 

    } 

    public string UserEmail 
    { 
     get 
     { 
      var userEmailClaim = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == "Application_UserEmail"); 
      if (string.IsNullOrEmpty(userEmailClaim?.Value)) 
      { 
       return null; 
      } 

      return userEmailClaim.Value; 
     } 
    } 
} 

UserClaimsPrincipalFactory.cs

//Override CreateAsync method to add your custom claim 
public override async Task<ClaimsPrincipal> CreateAsync(User user) 
{ 
    var claim = await base.CreateAsync(user); 
    claim.Identities.First().AddClaim(new Claim("Application_UserEmail", user.EmailAddress)); 
    return claim; 
} 
+0

感谢阿尔珀。这样,添加到会话的值将被更新为令牌? –

+0

自定义字段不会影响标记。 –