2016-10-04 110 views
0

我有一個ASP.NET MVC項目使用LDAP來驗證用戶。在開發服務器上,身份驗證正在工作,但在生產中它不起作用。LDAP - 無法訪問目錄

我在生產之前部署了項目,LDAP沒有問題。然後我再次部署,但在部署之前我更新了NuGet程序包管理器中的所有程序包。但它不起作用。

在我使用更新的軟件包部署新項目之前,我已經在開發服務器上測試過它,沒有出現任何問題。看來問題在於我無法訪問生產服務器中的LDAP路徑。

這裏是<appSettings>部分:

<appSettings> 
    <add key="FolderPath" value="Files/" /> 
    <add key="DirectoryPath" value="LDAP://blabla.bla.bla:389/CN=Users,DC=blabla,DC=bla,DC=bla" /> 
    <add key="DirectoryDomain" value="bla" /> 
    <add key="UserPermission" value="blabla" /> 
</appSettings> 

下面是驗證碼

public bool AuthenticateUser(string domain, string username, string password, string LdapPath, string userPermission) 
{   
    string domainAndUsername = domain + @"\" + username; 

    DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password); 

    try 
    { 
     Object obj = entry.NativeObject; 

     DirectorySearcher search = new DirectorySearcher(entry); 
     search.Filter = "(SAMAccountName=" + userPermission + ")"; 
     search.PropertiesToLoad.Add("cn"); 

     SearchResult result = search.FindOne(); 

     if (null == result) 
     { 
      return false; 
     } 

     LdapPath = result.Path; 
     string _filterAttribute = (String)result.Properties["cn"][0]; 

     if (_filterAttribute != userPermission) 
      return false; 
    } 
    catch (Exception ex) 
    { 
     return false; 
     throw new Exception("Error authenticating user. " + ex.Message); 
    } 

    return true; 
} 

開發和生產之間的唯一區別設置是DirectoryPathUserPermission值。

對不起,我的英語不好。

謝謝。

+0

執行它時會出現什麼錯誤? – A3006

+0

@Anand對不起,我沒有在生產服務器上記錄錯誤。我認爲問題出現在DirectoryEntry中,因爲我試圖在新項目上登錄3次,然後將項目更改爲舊項目,然後重新登錄,然後運行。我這樣做是因爲我想查看DirectoryEntry是否在新項目中傳遞,因爲如果我在LDAP中輸入錯誤的3次傳遞,帳戶將被鎖定,因此即使將項目更改爲新項目,我也無法登錄。我希望你明白。謝謝。 – TeachMe

回答

0

愚蠢的問題首先,是開發和生產服務器使用相同的DNS?
如果不是,可以通過生產服務器使用的DNS解析「blabla.bla.bla」(只需嘗試ping「blabla.bla.bla」)?

如果DNS無法解析名稱,那麼它將無法連接。