2013-04-20 55 views
1

在Internet mvc 4项目中,当以调试模式运行项目并登录到应用程序时,用户的角色是之前分配将会被认可。我使用的获取用户角色的功能是:System.Web.Security.Roles.GetRolesForUser(currentUser.Identity.Name)只有一次工作正确

System.Web.Security.Roles.GetRolesForUser(currentUser.Identity.Name) 

但是当我停止调试,并再次运行在调试模式下的应用程序,用户进行身份验证,但GetRolesForUser返回字符串[0]。为什么它发生并且不返回角色?

回答

1

我有同样的问题,我固定它改变它找到角色的方式:

var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new PlutusDbContext())); 

var user = manager.FindByName(currentUser.Identity.Name); 
var userRoles = manager.GetRoles(user.Id); 

PS:我使用Microsoft.AspNet.Identity 2.0.1