【问题标题】:IIS Virtual Directory/Application & Forms authenticationIIS 虚拟目录/应用程序和表单身份验证
【发布时间】:2010-05-30 00:53:17
【问题描述】:

我已经使用 .NET 4 设置并部署了一个简单的表单身份验证网站。

我在 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时,我被发送回登录页面,我已经登录了?

所以我很困惑我的问题是什么!我应该成功通过身份验证,然后重定向回目录,或者至少能够在我登录后手动查看它吗?

【问题讨论】:

  • 你解决过这个问题吗?我遇到了同样的问题。
  • 否 不幸的是,我还没有回到在 MVC 中使用基本表单身份验证,但我希望为时过早,如果我再次遇到这个问题,我会回到这篇文章。

标签: authentication forms iis-7


【解决方案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 应用程序 web.config 文件中添加以下内容:

<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");
}

【讨论】:

    【解决方案2】:

    您需要添加代码以在登录后从您的登录页面中重定向到查询字符串中注明的“ReturnUrl”URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      • 2012-11-09
      相关资源
      最近更新 更多