如何使用c#使用其凭据连接到LDAP服务器
问题描述:
我正尝试使用以下代码连接到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
它给出以下错误信息。 “服务器不可操作”。
答
如果您使用.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
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