2010-05-30 150 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"); 
} 
相关问题