2013-12-10 16 views
11

我發現如果我將用戶添加到ASP身份中的某個角色,直到我註銷並重新登錄後纔會生效。是否需要執行某些操作來刷新用戶的角色而不強制用戶先註銷?MVC 5 AddToRole在它工作之前需要註銷?

以下是我將用戶添加到角色的方式。

var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
var userId = HttpContext.Current.User.Identity.GetUserId(); 

userManager.AddToRole(userId, roleName); 

然後,我幾乎立即將用戶重定向到此操作方法。我可以告訴數據庫我已經加入了正確的角色,但是MVC仍然會將我重定向到登錄頁面。但是,如果我註銷,重新登錄,並嘗試轉到此操作方法,則它工作得很好。

[Authorize(Roles = RecoveryStandardRoles.ServiceProvider)] 

    public partial class CertifyController : Controller 

{ 
    #region Public Methods and Operators 

    public virtual ActionResult CompanyProfile() 
    { 
     return this.View(); 
    } 

    #endregion 
} 

謝謝你花時間看我的問題!

+1

如果你檢查,如果有人在你的代碼的作用,'User.IsInRole(角色名)'需要註銷和登錄,以反映被添加到新的角色。 'UserManager.IsInRole(userID,roleName)'不。 – nmit026

回答

7

MVC5通過使用註冊新用戶,分配角色和激活與角色用戶而不註銷,然後再打開:等待UserManager.AddToRoleAsync(user.Id, 「角色名稱」)

if (ModelState.IsValid) 
{ 
    var user = new ApplicationUser() { UserName = model.Email, Email = model.Email,StandName = model.StandName,FirstName = model.FirstName,LastName = model.LastName,CellPhone = model.CellPhone,Supervisor = model.Supervisor}; 
    IdentityResult result = await UserManager.CreateAsync(user, model.Password); 

    var roleStore = new RoleStore<IdentityRole>(context); 
    var roleManager = new RoleManager<IdentityRole>(roleStore); 

    var userStore = new UserStore<ApplicationUser>(context); 
    var userManager = new UserManager<ApplicationUser>(userStore); 


    if (result.Succeeded) 
    { 
     ***await UserManager.AddToRoleAsync(user.Id, "Users Tammy");*** 
     await SignInAsync(user, isPersistent: false); 
+0

這個答案幫助我解決了這個問題。我有一行將用戶添加到**線後的角色**。一旦我在**登錄之前添加角色**,角色就會添加,而不需要簽出/簽入。 – hatmatbbat10

+0

創建問題後的幾年中,這是我使用最多的答案。關鍵是確保你有'AddToRoleAsync' _before_'SignInAsync' –

5

ASP.NET身份使用聲明來存儲角色,並使用聲明而不是在每次需要執行授權時執行數據庫查詢。所以這些角色將不會出現在索賠中,直到該人再次登錄。你可以閱讀約using claims in ASP.NET Identity here。這些文章顯示瞭如何在登錄過程中添加聲明。但是,如果您將角色添加到當前用戶,則可以更新聲明using the method described in my answer to this QA而不強制用戶再次登錄。有分配給用戶的每個角色的聲明。添加新角色時使用ClaimTypes.Role。

+0

這沒有意義,因爲我的索賠表中沒有任何內容。 AddUserToRole爲AspNetUserRoles添加記錄,AspNetUserClaims中沒有任何記錄。你能解釋一下嗎? –

+0

另外,您引用的答案中的AuthenticationManager.SignIn行不能編譯。 –

+1

我所指的索賠不在數據庫表中。他們在CurrentPrincipal中。 –

6

@ kevin-junghans你的回答讓我得到正確的答案。此代碼顯示瞭如何將用戶添加到MVC 5中的角色並使該角色自動生效。

var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
var userId = HttpContext.Current.User.Identity.GetUserId(); 

userManager.AddToRole(userId, roleName); 

var authenticationManager = HttpContext.Current.GetOwinContext().Authentication; 

authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); 

var user = userManager.FindById(userId); 
var identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); 

authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity); 
+1

您正在有效地將人員記錄下來並重新登錄,這將起作用。方法CreateIdentity將再次添加角色作爲聲明,有效地添加新角色。我所描述的方法可能會回答您並不需要再次查詢數據庫來填充這些聲明。 –

2

向當前用戶添加角色後,您可以更新聲明而不強制用戶註銷並重新登錄。參考

Dim owinAuth = HttpContext.Current.GetOwinContext().Authentication 
Dim authResult = Await owinAuth.AuthenticateAsync(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie) 

authResult.Identity.AddClaim(New System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Role, "RoleName")) 

等效C#代碼:

var owinAuth = HttpContext.Current.GetOwinContext().Authentication; 

var authResult = 
    await owinAuth.AuthenticateAsync(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie); 

authResult.Identity.AddClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Role, roleName));