2016-09-04 9 views
-1

我想從我的C#應用​​程序連接到我的AD。發現了一些好文章,但沒有人helped.Found這段代碼,但它不工作對我來說,可能是我在我的連接字符串犯了一個錯誤:我對Active Directory非常陌生,因此難以通過LDAP從我的C#應用​​程序連接到它

DirectoryEntry ldapConnection = new DirectoryEntry("TK5-RED-DC-35.redmond.corp.microsoft.com", "fareast\v-sm262", "[email protected]"); 
ldapConnection.Path = "LDAP://DC=redmond,DC=corp,DC=microsoft,DC=com"; 

基本上我需要的是搜索用戶通過他們的別名在整個目錄中,因爲我對AD的概念非常陌生,所以我甚至不知道我的情況下域控制器是什麼。我附上我的廣告片斷。請有人幫我弄清楚我的情況下的連接字符串。

enter image description here

回答

1

既然你在.NET 3.5及以上,你應該看看System.DirectoryServices.AccountManagement(S.DS.AM)命名空間。

基本上,你可以定義域範圍內,並可以輕鬆地查找用戶和/或組AD:

// set up domain context - without further parameters, this will connect 
// to the default domain that you're connected to 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // find a user 
    Principal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); 

    if(user != null) 
    { 
     // do something here....  
    } 
} 

新S.DS.AM使得它很容易玩的用戶和組AD !

瞭解更多關於在這裏:​​

+0

我看到你建議的文章,但是你能告訴我一點關於連接字符串的形成? – dotnetman

+0

@SiddharthMishra:這些是[LDAP專有名稱](https://msdn.microsoft.com/en-us/library/aa366101(v = vs.85).aspx)語法,您需要學習和理解以便與AD有效合作 –

+0

非常感謝您的幫助,但是我有一個問題,看看我上面的AD的剪貼板,你能弄清楚我應該在域名下指定什麼,以便我可以在整個目錄中爲用戶進行搜索..請建議 – dotnetman

相關問題