我正在使用 ASP.NET MVC、Identity2。
我添加了“名字”自定义ClaimPrincipal
:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, long> manager)
{
var userIdentity = await manager.CreateIdentityAsync(
this,
DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity.AddClaim(new Claim("FirstName", FirstName));
return userIdentity;
}
如果我更新“FirstName”的值,我需要注销并重新登录,才能更新“FirstName”声明。是否可以使“FirstName”声明无效,从而强制刷新其值?
我见过这个问题 https://stackoverflow.com/questions/24587414/how-to-update-a-claim-in-asp-net-identity,它显示了如何更新声明的值,我想知道是否有更简单的方法来使它们无效。
当查看 MS 内置模板时,我注意到他们总是调用SignInManager.SignInAsync
,更改用户凭据(例如密码、2 因素身份验证等)后。
我还注意到Claims
一旦用户注销并重新登录,就会更新...所以在更改存储在 a 中的“FirstName”之后Claim
, 我打了电话SignInManager.SignInAsync
重新登录用户...这样,Claims
已更新:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> UpdateFirstName(string firstName)
{
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<long>());
user.FirstName = firstName;
// update FirstName which is stored in a Claim
var result = await UserManager.UpdateAsync(user);
if (result.Succeeded)
{
// re-signin the user, to refresh the Claims
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
// you need to redirect after calling SignInAsync, so claims are refreshed
return RedirectToAction("Index");
}
// add some error message...
return View();
}
注意:如问题所示,我正在存储Claims
在饼干中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)