2017-08-17 184 views
3

我正致力於使用我的公司AD對用戶進行身份驗證。此代碼正在工作,但需要超過25-30秒才能返回DirectorySearcher結果。我能做些什麼來改善響應時間?System.DirectoryServices很慢

public bool ADauthentication(string userName,string password) 
     { 
      try 
      { 
       string domain = ConfigurationManager.AppSettings["DirectoryDomain"]; 
       string path = ConfigurationManager.AppSettings["DirectoryPath"]; 
       string domainAndUserName = domain + @"\" + userName; 
       DirectoryEntry entry = new DirectoryEntry(path+"CN=Users,DC=myDomain,DC=com", userName, password); 
       entry.AuthenticationType = AuthenticationTypes.Secure; 
       DirectorySearcher search = new DirectorySearcher(entry); 
       search.Filter = "(SAMAccountName=" + userName+")"; 
       search.PropertiesToLoad.Add("CN"); 
       SearchResult result = search.FindOne(); 
       if (result == null) 
       { 
        return false; 
       } 
       return true; 
      } 
      catch(Exception ex) 
      { 
       log.Error($"Error: {ex.ToString()}"); 
       return false; 
      } 
     } 
+0

一種方法是,以減少目錄中的用戶數。 – itsme86

回答

0

我遇到了與AD類似的問題,但我通過緩存結果解決了這個問題。您可以創建一些後臺進程來同步AD和您的數據源。