asp.net-core - 扩展 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
(从HttpContext
or ControllerBase
)身份不是我在登录用户时创建的身份,而是 plain ClaimsIdentity
。
有什么办法可以改变吗?
解决方案
推荐阅读
- wordpress - 如何通过查看我的网站的移动版本来解决问题?
- c++ - 出现 C++ C26495 警告但我不知道如何解决
- javascript - Math.random() 在 nodejs 的浏览器中不起作用
- python - 如何将vs code python输出插入Microsoft access?
- vue.js - 如何取消移位/推送图像以在 vue 中列出
- amazon-web-services - 通过 Lambda 启动 EC2 实例时未执行 Powershell 用户数据
- python - 如何在 Python 上使用单个标准输出运行多个带有名称标签的程序?
- reactjs - 路由更改时如何修复 Switch 不更新视图?
- python - 使用 for 循环和直接方法的列表列表中的初始化错误
- simulator - 是否有任何红黑树模拟器可以重新创建我们想要的树?