我在 MVC 6 (Asp.Net One Core) 中有一个 Web 应用程序,并且我正在使用基于声明的身份验证。在登录方法中,我设置了声明:
var claims = new Claim[]
{
new Claim("Name", content.Name),
new Claim("Email", content.Email),
new Claim("RoleId", content.RoleId.ToString()),
};
var ci = new ClaimsIdentity(claims, "password");
await HttpContext.Authentication.SignInAsync("Cookies", new ClaimsPrincipal(ci));
现在,例如,如果用户更改了用户配置文件中的电子邮件,我该如何更改“电子邮件”声明的电子邮件值?我必须再次 SignOutAsync 和 SignInAsync 才能更新 cookie?最好的解决方案是将其保存到经典 session 中?有更好的解决办法吗?我完全错了?
有什么建议吗?
最佳答案
替代 SignOutAsync
和 SignInAsync
的另一种选择是使用 RefreshSignInAsync
。
例子:
var user = await _userManager.FindByIdAsync(yourId);
await _signInManager.RefreshSignInAsync(user);
查看SignInManager
中的RefreshSignInAsync
代码(net 5.0.8):
https://github.com/dotnet/aspnetcore/blob/ae2eabad0e49302d0632a7dde917fdc68d960dc4/src/Identity/Core/src/SignInManager.cs#L170
关于c# - 更新 ASP.NET One Core 中的声明值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39026796/