在ASP.NET One Core中更新声明值

问题描述 投票:9回答:2

我在MVC 6(Asp.Net One Core)中有一个Web应用程序,我正在使用基于声明的身份验证。在Login方法中,我设置了声明:

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));

现在,如果用户例如更改了用户配置文件中的电子邮件,我该如何更改“电子邮件”声明的电子邮件值?为了更新cookie,我必须再次使用SignOutAsync和SignInAsync吗?最好的解决方案是将其保存到经典会话中吗?还有更好的解决方案吗?我完全错了?

有什么建议?

c# asp.net asp.net-mvc claims-based-identity asp.net-identity-3
2个回答
4
投票

为了更新cookie,我必须再次使用SignOutAsync和SignInAsync吗?

答案是肯定的。

最简单的方法是您可以在更新电子邮件的同一操作方法中手动注销和登录(再次创建声明)。

最好的解决方案是将其保存到经典会话中吗?

我建议不要这样做。在ASP.Net MVC中明确使用会话状态是一种不好的做法。


25
投票

另一个选择,而不是SignOutAsyncSignInAsync,是使用RefreshSignInAsync

例:

var user = await _userManager.FindByIdAsync(yourId);
await _signInManager.RefreshSignInAsync(user);

查看RefreshSignInAsync中的SignInManager代码:https://github.com/aspnet/AspNetCore/blob/79beaea734016e86e83d0a249ab8b4c8bdf2046d/src/Identity/Core/src/SignInManager.cs

© www.soinside.com 2019 - 2024. All rights reserved.