扩展ClaimsAbpSession [英] Extend ClaimsAbpSession

查看:197
本文介绍了扩展ClaimsAbpSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要扩展ClaimsAbpSession并创建要在angular app和服务器之间的请求中发送的新属性.

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.

推荐答案

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;
}

这篇关于扩展ClaimsAbpSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆