2013-04-20 153 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