2012-08-12 41 views
-2

我正在更改MVC3項目中的默認LogOn函數,以便根據用戶的role使用User.IsInRole()將用戶重定向到某個頁面。當我測試了這些之後,第一對情侶用戶按照預期重新定向,但在此之後,我有一對夫婦沒有重定向到他們應該在的地方(他們通過所有的聲明並且打到了默認的家庭索引。)完全隨機,有時我的admin將被帶到管理頁面,其他時間不會。表單身份驗證User.IsInRole()在LogOn中隨機不工作

我的登錄功能:

[HttpPost] 
public ActionResult LogOn(LogOnModel model, string returnUrl) 
{ 
    if(ModelState.IsValid) 
    { 
     if(Membership.ValidateUser(model.UserName, model.Password)) 
     { 
      FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 
      if(Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && 
       returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && 
       !returnUrl.StartsWith("/\\")) 
      { 
       return Redirect(returnUrl); 
      } 
      else 
      { 
       if(User.IsInRole("Admin") || User.IsInRole("SuperAdmin")) 
       { 
        return RedirectToAction("Index", "Admin"); 
       } 
       else if(User.IsInRole("Employee")) 
       { 
        return RedirectToAction("Index", "Employee"); 
       } 
       else if(User.IsInRole("Accounting")) 
       { 
        return RedirectToAction("Index", "Accounting"); 
       } 
       // If the user is in none of those roles, send them to the home index 
       return RedirectToAction("Index", "Home"); 
      } 
     } 
     else 
     { 
      MembershipUser user = Membership.GetUser(model.UserName); 
      if(user == null) 
       ModelState.AddModelError("", "The user name or password provided is incorrect."); 
      else 
       ModelState.AddModelError("", "You haven't been approved yet, or you are locked out."); 
     } 
    } 
    // If we got this far, something failed, redisplay form 
    return View(model); 
} 

縱觀智能跟蹤,好像有時候它並沒有刻意去查詢數據庫,例如,它工作時我看到Execute Reader "dbo.aspnet_UsersInRoles_GetRolesForUser",當它沒有我別。

有誰知道爲什麼User.IsInRole()會返回false,即使用戶在角色中?是否有某種形式的兌現發生,爲什麼它不是每次都在對數據庫進行排隊呢?

我知道肯定用戶在我正在測試的角色中,並且我知道它並不試圖重定向到返回url,我也知道角色沒有存儲在任何cookie中。任何想法將不勝感激,我敢肯定,我可以用另一種方式去做,但現在我更關心爲什麼這種簡單的方法不起作用。

更新

我發現,如果我有一個重定向到被稱爲分揀另一個動作更換If(User.IsInRole(...語句,我添加了if語句出現,它工作時間的100%。

public ActionResult Sorting() 
{ 
    if(User.IsInRole("Admin") || User.IsInRole("SuperAdmin")) 
    { 
     return RedirectToAction("Index", "Admin"); 
    } 
    else if(User.IsInRole("Employee")) 
    { 
     return RedirectToAction("Index", "Employee"); 
    } 
    else if(User.IsInRole("Accounting")) 
    { 
     return RedirectToAction("Index", "Accounting"); 
    } 
    // If the user is in none of those roles, send them to the home index 
    return RedirectToAction("Index", "Home"); 
} 

因此很明顯,User.Identity.Name未設置(或將不返回用戶名),直到LogOn函數退出。它是否正確?我想,Membership.ValidateUser被稱爲用戶被認證後,顯然不是。

那麼Membership.ValidateUser()被調用後的什麼時候,User.IsInRole()會正常工作?是在Cookie被丟棄之後,還是什麼?

我想我可以使用if(Roles.IsUserInRole(model.UserName, "Admin")),因爲我確實有提交模型的用戶名。你認爲這是一個更好的主意,或者像我一樣使用Sorting重定向?

+0

沒有什麼似乎是錯在這裏..嘗試適當地再一次考驗。你不確定你在這裏。您需要找出您的應用程序無法正常工作的原因。你困惑着包括你自己在內的每個人。 – Rajesh 2012-08-12 04:22:10

+0

我實際上遇到了與Garrett相同的問題(基於LogOn操作中的登錄角色重定向)。我的解決方案是直接查詢數據庫...這很容易,所以爲什麼不行。是什麼樣子我就是這個Cookie還沒有設置,但使用'User.IsInRole(「嗒嗒」)的額外好處'似乎並不值得額外的故障排除時直接DB查詢完美。如果這聽起來像是一個可行的選擇,讓我知道,我會發佈一個代碼的例子作爲答案,所以你可以接受它! – Jared 2012-08-12 04:35:30

+0

@Jared,我開始認爲這與cookie沒有設置有關。看看我的更新,我提出了另外兩個解決方案。除了解決方案之外,我更感興趣的是爲什麼它沒有工作。如果你有證據證明它是cookie,我會接受這個答案。謝謝 – 2012-08-12 05:12:44

回答

0

問題是,當調用LogOn函數時,傳入的請求沒有經過身份驗證的用戶,User對象爲空,直到發出新的請求以填充User對象爲止。所以在LogOn中調用User.IsInRole()時,Usernull

簡而言之,User對象在執行請求的ASP.NET頁面的代碼之前,先在ASP.NET管道中設置,長 。現在,在後續訪問 時,ASP.NET運行時將會看到表格 身份驗證票證和User.Identity.IsAuthenticated將爲真, 但不是此請求。
...

此外,您將無法從User.Identity.Name獲取用戶名 。相反,使用LoginControlID。用戶名。

http://forums.asp.net/post/1988606.aspx