2013-10-09 128 views
1

美好的一天!登錄頁面登錄後再次出現MVC3

現在這是我的問題在我的項目。每次我登錄它總是在登錄頁面重定向。我試圖在我的控制器中放置一個斷點,它發生在首次登錄時,當我登錄並在我的控制器中單擊f10時,它會處於所有狀態。但是當我第二次登錄時,它會進入控制器並且該帳戶的角色是什麼。

我的代碼:

帳戶控制:

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); 
        } 
        if (Roles.IsUserInRole("Employer")) 
        { 
         return RedirectToAction("CustomerIndex", "Customer"); 
        } 
        else if (Roles.IsUserInRole("Worker")) 
        { 
         return RedirectToAction("WorkerIndex", "Worker"); 
        } 
        else if (Roles.IsUserInRole("Administrator")) 
        { 
         return RedirectToAction("ClientIndex", "Client"); 
        } 

       } 
       else 
       { 
        ModelState.AddModelError("", "The user name or password provided is incorrect."); 
       } 
      } 

      // If we got this far, something failed, redisplay form 
      return View(model); 
     } 

LogOn.cshtml:

@using (Html.BeginForm()) 
    { 
     <div style="width: 500px;"> 
      <fieldset> 
       <legend>Account Information</legend> 
       <div class="editor-label" style="padding-top: 20px"> 
        @Html.LabelFor(m => m.UserName) 
       </div> 
       <div class="editor-field"> 
        @Html.TextBoxFor(m => m.UserName) 
        @Html.ValidationMessageFor(m => m.UserName) 
       </div> 
       <div class="editor-label"> 
        @Html.LabelFor(m => m.Password) 
       </div> 
       <div class="editor-field"> 
        @Html.PasswordFor(m => m.Password) 
        @Html.ValidationMessageFor(m => m.Password) 
       </div> 
       <div class="editor-label"> 
        @Html.CheckBoxFor(m => m.RememberMe) 
        @Html.LabelFor(m => m.RememberMe) 
       </div> 
       <p> 
        <input type="submit" value="Log On" class="styledButton" /> 
       </p> 
      </fieldset> 
     </div> 

    } 

謝謝。 :D

回答

1

這是因爲用戶在執行該操作時(因爲您在那時登錄)尚未在HttpContext中設置,所以即使正在記錄日誌的用戶對Roles.IsUserInRole的調用也將全部爲return false屬於那個角色。

假設您正在使用您認識爲管理員角色成員的用戶進行日誌記錄。 如果您在檢查重定向角色之前將以下調試語句添加到LogOn方法中,您可以自己查看它。

Debug.WriteLine("Controller user name: " + User.Identity.Name); 
Debug.WriteLine("HttpContext user name: " + HttpContext.User.Identity.Name); 
Debug.WriteLine(Roles.IsUserInRole("Administrator")); 

你會在你的loggedIn即使在Visual Studio中的調試輸出窗口中看到並設置身份驗證cookie時,用戶名是空的isUserInRole和將返回false。

但是,當您再次提交表單時,由於用戶已經登錄(您剛纔沒有將他重定向到另一個頁面),用戶現在被設置在HttpContext中。 因此,這次如果您在LogOn方法的開頭添加這些調試行,您將在輸出窗口中看到用戶名並且他是管理員。

要解決您的問題,最簡單的方法是使用IsUserInRole overload,它可以讓您指定用戶名,而不是依賴HttpContext用戶。你會檢查用戶角色:

if (Roles.IsUserInRole(model.UserName, "Employer")) 
{ 
    return RedirectToAction("CustomerIndex", "Customer"); 
} 
else if (Roles.IsUserInRole(model.UserName, "Worker")) 
{ 
    ... 

你也可以自己創建一個主體,並將其設置爲HttpContext.User中檢查用戶角色之前:

var curIdentity = new System.Security.Principal.GenericIdentity(model.UserName); 
var curPrincipal = new System.Security.Principal.GenericPrincipal(curIdentity, null /*userRoles*/); 
HttpContext.User = curPrincipal; 
if (Roles.IsUserInRole("Employer")) 
{ 
    return RedirectToAction("CustomerIndex", "Customer"); 
} 
else if (Roles.IsUserInRole("Worker")) 
{ 
    ... 

但是我會保持簡單,只需將用戶名傳遞給IsUserInRole方法即可。對於您的應用程序中的任何其他操作,您可以依賴在HttpContext中爲您自動設置的用戶。

+0

哦,geez !!!謝謝你幫助我! –

+0

@VincentGumera,很高興能有幫助! :) –

+0

豎起大拇指給你! :)) –