Asp.net身份验证:在登录状态下更改用户角色

4

我有一个页面,用户登录后执行一些操作,根据这个操作,我会更改用户的角色,代码如下:

    var userStore = new UserStore<IdentityUser>();
    var manager = new UserManager<IdentityUser>(userStore);

    IdentityUser user = manager.FindById(TheMembershipID);

    manager.RemoveFromRole(user.Id, "StartUser");
    manager.AddToRole(user.Id, "AppUser");

然后,在客户端上,会重定向到需要AppUser角色身份验证的另一页。问题是该用户仍显示为StartUser登录状态。

在用户已登录情况下如何更改其角色?

1个回答

4
你需要让他们退出并重新登录,新角色才能生效。在你的代码之后:
//Get the authentication manager
var authenticationManager = HttpContext.GetOwinContext().Authentication;

//Log the user out
authenticationManager.SignOut();

//Log the user back in
var identity = manager.CreateIdentity(user,DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties() { IsPersistent = true}, identity);

这并非完全准确,但应该能让你大致了解。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接