我正在尝试实现ActiveDirectoryMembership提供程序,以便对活动目录使用表单身份验证。
我可以浏览到应用程序,并被重定向到签名页面。如果输入了错误的密码,就会得到正确的错误。如果输入正确的密码,它会将我重定向到默认的url (/Secure/Default.aspx),但会立即被重定向回signin页面。我可以看到这两个重定向,因为我使用的是小提琴。因此,我确信它对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
....
}发布于 2012-11-30 05:31:31
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文件夹确实是您希望通过登录保护的唯一文件夹,那么上面的这些都能工作,但是如果您想锁定除登录页面之外的所有内容,您只需在主<authorization>部分中使用<deny users "?" />。
https://stackoverflow.com/questions/13639296
复制相似问题