参考 stackoverflow 上 How do I add a custom claim to authentication cookie 问题的回答,通过下面的代码解决了
var authenticateResult = await HttpContext.AuthenticateAsync();
if (authenticateResult.Succeeded)
{
var claimsIdentity = authenticateResult.Principal.Identity as ClaimsIdentity;
if (claimsIdentity != null)
{
if (!claimsIdentity.HasClaim(c => c.Type == CnblogsClaimTypes.BlogId))
{
claimsIdentity.AddClaim(new Claim(CnblogsClaimTypes.BlogId, user.BlogId.ToString()));
await HttpContext.SignInAsync(authenticateResult.Principal, authenticateResult.Properties);
}
}
}
重新生成cookie 不就行了么?
我想找到更优雅的方法
参考资料:ASP.NET Core: Three(+1) ways to refresh the claims of a logged-in user
– dudu 1年前参考资料:Claims Transformation in .NET 6
– dudu 1年前