我想实现一个通过Ldap连接到AD的函数。
我的代码:
public bool TryConnect(string server, string userName, string password)
{
try
{
var credentials = new NetworkCredential(@"test\administrator123", @"P2ssw0rd");
var ldapidentifier = new LdapDirectoryIdentifier("Test");
using (var ldapconn = new LdapConnection(ldapidentifier, credentials))
{
ldapconn.AuthType = AuthType.Basic;
var request = new SearchRequest("dc=test, dc=local", "(objectClass=user)", System.DirectoryServices.Protocols.SearchScope.Subtree, null);
var response = (SearchResponse)ldapconn.SendRequest(request);
}
}
catch (Exception)
{
return false;
}
return true;
}
这是有效的,但我不喜欢它!
如果没有try / catch而没有一些虚拟搜索请求,是否有一些更简洁的方法来测试连接?