2011-12-21 49 views
0

我正嘗試使用以下代碼連接到ldap服務器。如何使用c#使用其憑據連接到LDAP服務器

 Dim xd As New XmlDocument() 
     Dim domainAndUsername As String = String.Empty 
     Dim userName As String = String.Empty 
     Dim passWord As String = String.Empty 
     Dim at As AuthenticationTypes = AuthenticationTypes.Anonymous 
     Dim sb As New StringBuilder() 



     domainAndUsername = "LDAP://MARS-AD\kumaravi:[email protected]:389/OU=EXD,OU=People,OU=EMEAI,OU=Mars,OU=IT-Services,DC=Mars-AD,DC=Net" 
     userName = "username" 
     passWord = "password" 

     at = AuthenticationTypes.Secure 

     'Create the object necessary to read the info from the LDAP directory 
     Dim entry As New DirectoryEntry(domainAndUsername, userName, passWord, at) 



     Dim mySearcher As New DirectorySearcher(entry) 
     Dim results As SearchResultCollection 
     mySearcher.Filter = filter 

它給出以下錯誤信息。 「服務器不可操作」。

+0

LDAP服務器的什麼樣的最好利用的新功能?活動目錄??你的LDAP路徑肯定是無效的 - 它應該是這樣的:'LDAP:// domain/OU = EXD,OU = People,OU = EMEAI,OU = Mars,OU = IT-Services,DC = Mars-AD,DC = Net'或類似的東西 - 肯定沒有在該LDAP字符串中的用戶名/密碼.... – 2011-12-21 10:21:30

回答

0

如果您使用.NET 3.5或更高版本,可以使用PrincipalSearcher和「查詢通過例如」主要做你的搜索:

// create your domain context - this will automatically connect to the 
// current domain of your machine 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// define a "query-by-example" principal - here, we search for a UserPrincipal 
UserPrincipal qbeUser = new UserPrincipal(ctx); 

// create your principal searcher passing in the QBE principal  
PrincipalSearcher srch = new PrincipalSearcher(qbeUser); 

// find all matches 
foreach(var found in srch.FindAll()) 
{ 
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....   
} 

您可以指定任何屬性UserPrincipal並將它們用作您的PrincipalSearcher的「查詢範例」。

如果您還沒有 - 絕對看MSDN文章Managing Directory Security Principals in the .NET Framework 3.5這很好地說明如何在System.DirectoryServices.AccountManagement