2013-02-11 128 views
35

我想用C#連接到我們的本地Active Directory。我發現this good documentation通過LDAP連接到Active Directory

但我真的不知道如何通過LDAP連接。

有人可以解釋如何使用被問到的參數嗎?

示例代碼:

static DirectoryEntry createDirectoryEntry() 
    { 
    // create and return new LDAP connection with desired settings 

    DirectoryEntry ldapConnection  = new DirectoryEntry("rizzo.leeds-art.ac.uk"); 
    ldapConnection.Path    = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk"; 
    ldapConnection.AuthenticationType = AuthenticationTypes.Secure; 
    return ldapConnection; 
    } 

我只是主機名和我們的Active Directory服務器的IP地址。 DC=xxx,DC=xx等等是什麼意思?

+4

ou =組織單位,dc =域組件 – paul 2013-02-11 13:58:46

回答

59

DC是您的域名。如果你想連接到域的example.com比你的直流是:DC =例如,DC = com

你實際上不需要你的域控制器的任何主機名或IP地址(可能有很多) 。

只是您要連接到域本身的映像。因此,要連接到域example.com,您只需編寫

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com"); 

然後您就完成了。

您也可以指定用戶和用於連接密碼:

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com", "username", "password"); 

而且一定要經常寫LDAP大寫。我遇到了一些麻煩和奇怪的例外,直到我在某處讀到我應該嘗試用大寫字母寫出來並解決了我的問題。

directoryEntry.Path屬性允許您更深入地瞭解您的域。所以如果你想在一個特定的OU(組織單元)中搜索一個用戶,你可以在那裏設置它。

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com"); 
directoryEntry.Path = "LDAP://OU=Specific Users,OU=All Users,OU=Users,DC=example,DC=com"; 

這將匹配以下AD層次結構:

  • COM
    • 例如
      • 用戶
        • 所有用戶
          • 特定用戶

只需層次寫從最深到最高。

Now you can do plenty of things

例如,通過賬戶名搜索用戶,並得到其姓:

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com"); 
DirectorySearcher searcher = new DirectorySearcher(directoryEntry) { 
    PageSize = int.MaxValue, 
    Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=AnAccountName))" 
}; 

searcher.PropertiesToLoad.Add("sn"); 

var result = searcher.FindOne(); 

if (result == null) { 
    return; // Or whatever you need to do in this case 
} 

string surname; 

if (result.Properties.Contains("sn")) { 
    surname = result.Properties["sn"][0].ToString(); 
} 
+1

這是一個答案!感謝您的幫助。 – 2013-02-11 15:32:57

+0

我有IpAddress和FQN;哪個更快? 1個IP地址是否還有超過1個域名參與其中? – 2015-07-06 12:19:59

+0

我遇到過各種異常情況,直到我將過濾器簡化爲:'「(cn = roland)」'。從工作系統中,可以使過濾器逐漸「更好」(=更復雜) – Roland 2016-01-22 17:33:31

2

ldapConnection是服務器地址:ldap.example.com Ldap.Connection.Path是ADS中您希望以LDAP格式插入的路徑。

OU = Your_OU,OU = other_ou,DC =例如,DC = COM

你開始在最深OU工作返回到AD的根部,然後添加DC = X爲每個域節,直到你擁有了一切,包括頂級域名

現在我錯過參數進行身份驗證,這個工程的相同用戶名

CN =用戶名,OU =用戶,DC =例如道路, DC = com

Introduction to LDAP

0

如果您的電子郵件地址是「[email protected]」,嘗試改變createDirectoryEntry(),如下。

XYZ是一個可選的參數,如果它的mydomain目錄中存在

static DirectoryEntry createDirectoryEntry() 
{ 
    // create and return new LDAP connection with desired settings 
    DirectoryEntry ldapConnection = new DirectoryEntry("myname.mydomain.com"); 
    ldapConnection.Path = "LDAP://OU=Users, OU=XYZ,DC=mydomain,DC=com"; 
    ldapConnection.AuthenticationType = AuthenticationTypes.Secure; 
    return ldapConnection; 
} 

這將基本上檢測COM - > MYDOMAIN - > XYZ - >用戶 - > ABCD

主要功能看起來如下:

try 
{ 
    username = "Firstname LastName" 
    DirectoryEntry myLdapConnection = createDirectoryEntry(); 
    DirectorySearcher search = new DirectorySearcher(myLdapConnection); 
    search.Filter = "(cn=" + username + ")"; 
    ....