0

我試圖實現ActiveDirectoryMembership提供商,所以我可以使用窗體身份驗證對Active Directory。ActiveDirectoryMembershipProvider總是重定向到簽到

我可以瀏覽到該應用程序,並重定向到登錄頁面。如果輸入不正確的密碼,我會得到正確的錯誤。如果我輸入正確的密碼,它重定向我的默認URL(/Secure/Default.aspx),但立即被重定向回到登錄頁面。我可以看到兩個重定向,因爲我使用的是提琴手。所以我確信它確實對AD進行了身份驗證,但仍然帶我回到登錄頁面。我也知道瀏覽器確實接受cookies,因爲我在應用程序中構建了一個測試頁來證明這一點。我已經包括在web.config及以下相關的代碼,只是想不通,我錯過了什麼?

編輯: 我發現,如果我指定的,而不是UseCookies UseUri,一切都開始工作。但是我已經驗證了我可以將數據存儲在一個頁面上的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 
    .... 
} 
+0

僅供參考,沒有必要調用'SetAuthCookie'當你已經使用'RedirectFromLoginPage'。當爲FormsAuth啓用Cookie時,RedirectFromLoginPage會自動調用SetAuthCookie。有關更多信息,請參見http://msdn.microsoft.com/en-us/library/ka5ffkce(v=vs.90).aspx。 – Sumo

回答

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>部分。