Extend ClaimsAbpSession

為{幸葍}努か 提交于 2019-12-13 03:23:09

问题


I need to extend the ClaimsAbpSession and create new properties to be sent in the requests between the angular app and the server.

Actually, I´ve stored these new properties using claims. But when the user refreshes the page, the values in the claims are lost.


回答1:


MyAppSession.cs

// Define your own session and add your custom field to it.
// Then, you can inject MyAppSession and use its 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;
}


来源:https://stackoverflow.com/questions/45802153/extend-claimsabpsession

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!