2016-02-17 97 views
3

我有一个页面,登录的用户更改用户角色执行的操作,并基于该我改变这样的用户角色:Asp.net身份:在登录

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身份登录。

如何在用户登录时更改用户角色?

感谢

回答

3

您需要登录出来,并回到了新角色才能生效。在您的代码后:

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

这不是确切的,但应该给你的一般想法。

+0

好的,谢谢你的回答 – frenchie