c# - 更新 ASP.NET One Core 中的声明值

标签 c# asp.net asp.net-mvc claims-based-identity asp.net-identity-3

我在 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 中?有更好的解决办法吗?我完全错了?

有什么建议吗?

最佳答案

替代 SignOutAsyncSignInAsync 的另一种选择是使用 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/

相关文章:

c# - 网页集合发生变化时如何重新加载RouteTable?

ASP.NET MVC ViewEngine ViewLocationCache.GetViewLocation 返回 null

c# - IHttpHandler : Speeding up performance with parallelism?

c# - 如何将 Controller 用于局部 View ?

javascript - 动态命名部分 div 以备后用?

c# - ASMX 网络服务 : 'anonymous types' error

c# - Visual Studio : c# interactive shell window

c# - C#XNA SoundEffect.FromStream

c# - 服务器端字符串的 HTML 编码问题

c# - 在 Asp.Net MVC 5 中使用 Entity Framework 6 代码优先方法调用存储过程