ActiveDirectoryMembershipProvider始终重定向到登录页面

时间:2012-11-30 05:00:41

标签: asp.net forms-authentication activedirectorymembership

我正在尝试实现ActiveDirectoryMembership提供程序,以便我可以对Active Directory使用表单身份验证。

我可以浏览到该应用程序,然后重定向到登录页面。如果我输入的密码不正确,我会收到正确的错误。如果我输入正确的密码,它会将我重定向到默认网址(/Secure/Default.aspx),但会立即重定向回登录页面。我可以看到两个重定向因为我正在使用小提琴手。所以我肯定知道它正确地验证了AD,但仍然让我回到了登录页面。我也知道浏览器确实接受了cookie,因为我在应用程序中构建了一个测试页来证明这一点。我已经在下面包含了web.config和相关代码,只是无法弄清楚我错过了什么......

修改 我发现如果我指定UseUri而不是UseCookies,一切都会开始工作。但我已经验证我可以将数据存储在一个页面上的cookie中,并在另一个页面上检索它,那么为什么它不能用于身份验证呢?

编辑2 我还从登录页面删除了我的代码并使用了标准登录控件,同样的问题。

Web.config文件:

<connectionStrings>
    <add name="ADConnectionString" connectionString="LDAP://YNET" />
</connectionStrings>
<system.web>      
    <authentication mode="Forms">
      <forms name=".ASPXAUTH" 
             path="/FormsAuth"
             loginUrl="~/SignIn.aspx" 
             defaultUrl="~/Secure/Default.aspx" 
             timeout="20" 
             requireSSL="false"
             protection="All"
             slidingExpiration="true"
             cookieless="UseCookies"
             enableCrossAppRedirects="false"/>
    </authentication>

    <authorization>
      <!-- Deny unauthenticated users will cause automatic redirect to the sign in page when using forms authentication. -->
      <deny users="?"/>
      <allow users="*"/>
    </authorization>

    <!-- For non AD passthrough authentication, specify the defaultProvider property -->
    <membership defaultProvider="ActiveDirectoryMembershipProvider">
      <providers>
        <clear/>
        <add name="ActiveDirectoryMembershipProvider"
             type="System.Web.Security.ActiveDirectoryMembershipProvider"
             connectionStringName="ADConnectionString"
             attributeMapUsername="sAMAccountName"/>

       </providers>      
    </membership>
</system.web>

登录页面:

bool bIsValid = System.Web.Security.Membership.ValidateUser(txtUsername.Text, txtPassword.Text);

//Authenticate the user credentials against the default membership provider specified in configuration
if (bIsValid)
{
    System.Web.Security.FormsAuthentication.SetAuthCookie(txtUsername.Text, true);

    System.Web.Security.FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true);

}
else
{
    //display error
    ....
}

1 个答案:

答案 0 :(得分:1)

Cookie问题(可能是登录问题)是由于您将Cookie路径设置为/FormsAuth。这意味着cookie仅对该URL路径有效,否则将被丢弃。此外,您可以稍微调整一下<authorization>部分,因为我已在部分Web.config的以下完整更新中进行了调整:

<connectionStrings>
    <add name="ADConnectionString" connectionString="LDAP://YNET" />
</connectionStrings>
<system.web>      
    <authentication mode="Forms">
      <forms name=".ASPXAUTH" 
             path="/"
             loginUrl="~/SignIn.aspx" 
             defaultUrl="~/Secure/Default.aspx" 
             timeout="20" 
             requireSSL="false"
             protection="All"
             slidingExpiration="true"
             cookieless="UseCookies"
             enableCrossAppRedirects="false"/>
    </authentication>

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

    <!-- For non AD passthrough authentication, specify the defaultProvider property -->
    <membership defaultProvider="ActiveDirectoryMembershipProvider">
      <providers>
        <clear/>
        <add name="ActiveDirectoryMembershipProvider"
             type="System.Web.Security.ActiveDirectoryMembershipProvider"
             connectionStringName="ADConnectionString"
             attributeMapUsername="sAMAccountName"/>

       </providers>      
    </membership>
</system.web>
<location path="Secure">
    <system.web>
        <authorization>
            <deny users="?"/>
        </authorization>
    </system.web>
</location>

如果/Secure文件夹确实是您要通过登录保护的唯一文件夹,那么上述工作方式有效,但如果您想锁定除登录页面以外的所有内容,则只需<deny users "?" />在您的主<authorization>部分。