2013-04-24 42 views
0

我想在創建它之前檢查用戶是否存在於Active Directory中。我使用下面的代碼:在C#中獲取活動目錄用戶

private static DirectoryEntry FindActiveDirectoryUser(string userName, string domainName) 
    { 
     using (DirectoryEntry domain = new DirectoryEntry("LDAP://" + domainName)) 
     { 
      using (DirectorySearcher searcher = new DirectorySearcher(domain)) 
      { 
       searcher.ReferralChasing = ReferralChasingOption.All; 
       searcher.Filter = "(sAMAAccountName=" + userName + ")"; 
       return searcher.FindOne().GetDirectoryEntry(); 
      } 
     } 
    } 

,我發現了錯誤

A referral was returned from the server. 

的變量userName和域名,我都嘗試FQDN和2000年以前的用戶名(如域\用戶) ,以及簡單的域名和用戶名稱。

有誰知道如何解決這個問題?

回答

0

試着在LINK找到你的答案。它會解決你的問題。

嘗試Link 2

嘗試Link 3

我覺得這個錯誤是由於您的LDAP字符串。

希望它有效。

+0

謝謝你,但如果你指的是主要回答這個線程 - 這實際上是我根據我的代碼。它沒有解決我收到的錯誤。 – user884248 2013-04-24 10:36:38

+0

確定嘗試鏈接2和鏈接3。 – Rahul 2013-04-24 10:47:34

+0

鏈接2實際上幫了我,但也 - 我看來我錯誤地寫下了屬性名稱。它是sAMAccountName,而不是sAMAAccountName(在那裏有一個額外的A)。 謝謝! – user884248 2013-04-24 12:34:13

2

嘗試這個one.It將工作

 public GetUserByLoginName(String userName) 
    { 


     try 
     { 
      using (HostingEnvironment.Impersonate()) 
      { 

       // This code runs as the application pool user 



       _directoryEntry = null; 
       string path = "LDAP://xxx.local/DC=xxx,DC=xxx"; //your LDAP Address 


       DirectorySearcher directorySearch = new DirectorySearcher(path); 
       directorySearch.Filter = "(&(objectClass=user)(SAMAccountName=" + userName + "))"; 
       SearchResult results = directorySearch.FindOne(); 


      } 

     } 

     catch (Exception ex) 
     { 
      return null; 
     } 
    } 
相關問題