2010-05-30 148 views
0

我設置和使用.NET 4IIS虛擬目錄/應用程序和Forms身份驗證

我創建了一個虛擬目錄部署一個簡單的窗體身份驗證的網站會員(現在轉換到「應用程序」)的IIS7並設置web.config文件中的虛擬目錄,如下所示:

<configuration>
<system.web>
<authorization>
<deny users="?">
</authorization>
</system.web>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>

太好了!我瀏覽到虛擬目錄:../mydomain/books/

,我自動重定向到web.config在我的根目錄中指定的登錄頁面和URL路徑被置於如下:

../Account/Login.aspx?ReturnUrl=%2fbooks

在這一點上,我成功登錄,但我沒有重定向到任何地方,當我手動返回到目錄../books,我被髮回到登錄頁面,我已經登錄了?

所以我很困惑我的問題是什麼!我應該成功通過身份驗證,然後重定向回目錄,或者至少能夠在登錄後手動查看它?

+0

你有沒有永遠解決這個?我遇到了同樣的問題。 – 2011-02-03 14:22:41

+0

不幸的是,我還沒有回到在MVC中使用基本表單身份驗證,但我希望太快,如果我再次遇到此問題,我會回到這篇文章。 – 2011-02-04 09:29:21

回答

0

在登錄後,您需要添加代碼以重定向到查詢字符串中記錄的「ReturnUrl」URL。

1

由於我不得不自己解決這個問題,所以我想我可以將它發佈給其他人,以便他們的搜索將它們帶到這裏。

這是使用表單身份驗證所需的一切,允許您的格式向匿名用戶公開,在現有.Net(.aspx)網站和MVC Web應用程序之間傳遞憑據並重定向到給定url登錄後。

使用任何你正在尋找的作品。

確保您的.Net Web應用程序(.aspx)的虛擬目錄/虛擬應用程序路徑不在Views目錄中。還要確保你在IIS中設置你的虛擬目錄/應用程序。

我用Entity Framework和Identity與SQLServer數據庫來驗證我的用戶。

虛擬應用程序/目錄。NET(的.aspx)web.config文件中需要包含此:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 

    <!-- other stuff --> 

    <system.web> 
     <authentication mode="Forms"> 
      <forms 
       loginUrl="login.aspx" 
       name=".AUTHCOOKIE" 
       protection="All" 
       path="/" 
       domain="your_domain.com" 
       enableCrossAppRedirects="true" 
       timeout="60"> 
      </forms> 
     </authentication> 

     <authorization> 
      <deny users="?" /> 
      <allow users="*" /> 
     </authorization> 

     <machineKey 
      validationKey="your validation key" 
      decryptionKey="your decryption key" 
      validation="SHA1" 
      decryption="AES" 
     /> 

     <!-- other stuff --> 

    </system.web> 

    <location path="/path/to/your/site.css"> 
     <system.web> 
      <authorization> 
       <allow users="?"></allow> 
      </authorization> 
     </system.web> 
    </location> 

    <!-- other stuff --> 

</configuration> 

然後,在你的後面頁面的Login.aspx代碼,你需要這樣的:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) 
{ 
    string username = Login1.UserName; 
    string pwd = Login1.Password; 

    /* do your authentication here 
     connect to user store 
     get user identity 
     validate your user 
     etc 
    */ 
    if (user != null) 
    { 
     FormsAuthentication.SetAuthCookie(username, Login1.RememberMeSet); 
     System.Web.HttpCookie MyCookie = System.Web.Security.FormsAuthentication.GetAuthCookie(User.Identity.Name.ToString(), false); 
     MyCookie.Domain = "your_domain.com"; 
     Response.AppendCookie(MyCookie); 
     Response.Redirect("~/path/to/your/index.aspx"); 
    } 
    else 
    { 
     StatusText.Text = "Invalid username or password."; 
     LoginStatus.Visible = true; 
    } 
} 

現在,在您的MVC應用程序的網頁。配置文件補充一點:

<configuration> 

    <!-- other stuff --> 

    <system.web> 
     <authentication mode="Forms"> 
      <forms 
       loginUrl="Account/Login" 
       name=".AUTHCOOKIE" 
       protection="All" 
       path="/" 
       domain="your_domain.com" 
       enableCrossAppRedirects="true" 
       timeout="30"/> 
     </authentication> 

     <authorization> 
      <deny users="?"/> 
      <allow users="*"/> 
     </authorization> 

     <machineKey 
      validationKey="your validation key" 
      decryptionKey="your decryption key" 
      validation="SHA1" 
      decryption="AES" 
     /> 

     <!-- other stuff --> 

    </system.web> 

    <location path="/path/to/your/site.css"> 
     <system.web> 
      <authorization> 
       <allow users="?"></allow> 
      </authorization> 
     </system.web> 
    </location> 

    <!-- other stuff --> 

    <system.webServer> 
     <modules runAllManagedModulesForAllRequests="true"> 
      <remove name="FormsAuthenticationModule"/> 
      <add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule"/> 
      <remove name="UrlAuthorization"/> 
      <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"/> 
     </modules> 
    </system.webServer> 

    <!-- other stuff --> 

</configuration> 

在你的MVC的AccountController登錄方法應該是這個樣子:

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
{ 
    if (ModelState.IsValid) 
    { 
     /* do your authentication here 
     connect to user store 
     get user identity 
     validate your user 
      etc 
     */ 
     if (user != null) 
     { 
      await SignInAsync(user, model.RememberMe); 
      FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe); 
      System.Web.HttpCookie MyCookie = System.Web.Security.FormsAuthentication.GetAuthCookie(User.Identity.Name.ToString(), false); 
      MyCookie.Domain = "your_domain.com"; 
      Response.AppendCookie(MyCookie); 

      if (Url.IsLocalUrl(returnUrl)) 
      { 
       return Redirect(returnUrl); 
      } 
      else 
      { 
       return RedirectToAction("Index", "Home"); 
      } 
     } 
     else 
     { 
      ModelState.AddModelError("", "Invalid username or password."); 
     } 
    } 

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

最後,你的MVC的AccountController註銷的方法是這樣的:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult LogOff() 
{ 
    AuthenticationManager.SignOut(); 
    FormsAuthentication.SignOut(); 
    return RedirectToAction("Login", "Account"); 
} 
相關問題