首页 > 解决方案 > 扩展 ClaimsIdentity?

问题描述

我正在尝试自定义ClaimsIdentity.

原因是我不想使用 cookie 来保存所有声明,因为在我的应用程序的上下文中,我有很多(数百个)Projects用户可以访问并且可以轻松地使 cookie 气球超过 4k(我认为是极限)

这是我的自定义课程

public class CustomIdentity : ClaimsIdentity
{
    public List<string> Projects { get; set; } = new List<string>();

    public CustomIdentity(List<Claim> claims, string authScheme)
        : base(claims, authScheme)
    {
        Projects.Add("test data");
    }
}

以及用户登录时如何创建身份:

var claims = new List<Claim>
{
     new Claim("Id", userId),
     new Claim(ClaimTypes.Email, email)
};

var claimsIdentity = new CustomIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

var authProperties = new AuthenticationProperties
{
    ExpiresUtc = DateTime.Now.AddHours(1),
    IsPersistent = true,
    IssuedUtc = DateTime.Now

};

await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);

问题是,每当我检索ClaimsPrincipal(从HttpContextor ControllerBase)身份不是我在登录用户时创建的身份,而是 plain ClaimsIdentity

有什么办法可以改变吗?

标签: asp.net-coreasp.net-core-mvcasp.net-identityasp.net-core-2.0claims-based-identity

解决方案


推荐阅读