2009-01-22 112 views
3

我正在編寫一些針對Active Directory的c#並試圖無休止地讓這個工作無效。下面的代碼工作,並遵循它的代碼不:c#針對通過LDAP的Active Directory

下面的代碼使用「WinNT://」+ Environment.MachineName +「,計算機」進行連接並工作正常。

DirectoryEntry localMachine = new DirectoryEntry 
     ("WinNT://" + Environment.MachineName + ",Computer"); 

    DirectoryEntry admGroup = localMachine.Children.Find 
     ("Administrators", "group"); 

    object members = admGroup.Invoke("members", null); 

    foreach (object groupMember in (IEnumerable)members) 
    { 
     DirectoryEntry member = new DirectoryEntry(groupMember); 
     output.RenderBeginTag("p"); 
     output.Write(member.Name.ToString()); 
     output.RenderBeginTag("p"); 
    } 



    base.Render(output); 

我現在試圖改變行:

"WinNT://" + Environment.MachineName + ",Computer" 

"LDAP://MyDomainControllerName" 

但似乎不管我在的地方值「MyDomainControllerName」它的價值是什麼不會工作。

要獲得'MyDomainControllerName'的值,我右鍵單擊MyComputer,並按照其他地方的建議複製計算機名稱的值,但這不起作用。


當我嘗試使用LDAP:上面這導致以下錯誤// RootDSE的選項:

位於路徑LDAP的Active Directory對象:// RootDSE的不是一個容器

這是您提到的成員方法的問題嗎?

+0

我不完全理解你的問題根據你的代碼示例。您是否試圖使用LDAP來枚舉本地組的成員身份?如果是這樣,那就行不通了。 – barneytron 2009-01-23 03:45:24

回答

6

當連接到AD使用.NET Framework,你可以使用「無服務器」綁定,或者您可以指定每次使用的服務器(服務器綁定)。

下面是同時使用的例子:

// serverless 
DirectoryEntry rootConfig = new DirectoryEntry("LDAP://dc=domainname,dc=com"); 

// server bound 
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://domainControllerName/dc=domainName,dc=com"); 

我想,你誤入歧途打算是你忘了,包括在FQDN上月底域。希望這可以幫助。

+0

當我右鍵點擊我的電腦,看看電腦名稱,它似乎是mypc.domain.net - 所以我嘗試LDAP://dc =域,dc = net,我試過LDAP:// mypc/dc =域,dc = net和兩個我得到一個錯誤,告訴我一個無效的dn語法已被指定。 一切順利 – 78lro 2009-01-26 10:38:21

0

您需要傳遞授權的用戶名和密碼。
嘗試設置:DirectoryEntry.Username和DirectoryEntry.Password

0

您是否試過指定端口號和其他參數?

我們的LDAP字符串看起來像:LDAP:// MYSERVER:1003/[email protected] | 1,OU =成員,O = mdhfw2

0

看起來您需要獲取LDAP連接信息。您可以調用LDAP:// RootDSE來獲取ASP.NET Wiki中顯示的信息。

請記住,LDAP對象沒有與WINNT對象相同的成員方法和屬性,所以不要指望group.Invoke(「members」)和其他函數完全相同。您還應該閱讀使用LDAP的DirectoryServices documentation

6

是的 - RootDSE不是一個容器 - 但它擁有一些有趣的屬性,你可以查詢 - 例如您的域控制器的名稱。

您可以通過使用這樣的代碼檢查這些了:

DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE"); 

if (deRoot != null) 
{ 
    Console.WriteLine("Default naming context: " + deRoot.Properties["defaultNamingContext"].Value); 
    Console.WriteLine("Server name: " + deRoot.Properties["serverName"].Value); 
    Console.WriteLine("DNS host name: " + deRoot.Properties["dnsHostName"].Value); 

    Console.WriteLine(); 
    Console.WriteLine("Additional properties:"); 
    foreach (string propName in deRoot.Properties.PropertyNames) 
    Console.Write(propName + ", "); 
    Console.WriteLine(); 
} 

或保存自己的麻煩,去抓住我的在C「Beavertail ADSI Browser」#源代碼 - 詳細顯示瞭如何連接到的RootDSE和它所提供。

相關問題