Update Claims values in ASP.NET One Core

早过忘川 提交于 2019-12-12 07:50:44

问题


I have a Web Application in MVC 6 (Asp.Net One Core), and I'm using Claims based authentication. In the Login method I set the Claims:

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

Now, if the user for example changes the email in the user profile, how can I change the e-mail value for the "Email" Claim? I have to SignOutAsync and SignInAsync again in order to update the cookie? The best solution is to save this into a classic session? There is a better solution? I'm totally wrong?

Any suggestions?


回答1:


I have to SignOutAsync and SignInAsync again in order to update the cookie?

Answer is yes.

Easiest way is you can manually sign-out and sign-in (create claims again) inside the same action method where you are updating the email.

The best solution is to save this into a classic session?

I suggest not to do that. Using session state explicitly is a bad practice in ASP.Net MVC.




回答2:


Another option, instead of SignOutAsync and SignInAsync, is to use RefreshSignInAsync.

Example:

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

View the RefreshSignInAsync code in the SignInManager: https://github.com/aspnet/AspNetCore/blob/79beaea734016e86e83d0a249ab8b4c8bdf2046d/src/Identity/Core/src/SignInManager.cs



来源:https://stackoverflow.com/questions/39026796/update-claims-values-in-asp-net-one-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!