Active directory 用C语言扩展LDAP查询
我目前有两种方法:Active directory 用C语言扩展LDAP查询,active-directory,ldap,active-directory-group,Active Directory,Ldap,Active Directory Group,我目前有两种方法: public ADHelper() { connection = InitializeConnection(); } private DirectoryEntry InitializeConnection() { DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://servername.doma
public ADHelper()
{
connection = InitializeConnection();
}
private DirectoryEntry InitializeConnection()
{
DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://servername.domain.com:389/DC=domain,DC=com");
ldapConnection.Username = "user"
ldapConnection.Password = "password";
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
return ldapConnection;
}
我想创建另一个方法来检查该域中是否存在对象。我目前正在通过以下方式进行这项工作:
public bool Exists(string objectPath)
{
bool found = DirectoryEntry.Exists("LDAP://" + objectPath);
return found;
}
但这迫使我指定整个LDAP字符串。我只想在Exists方法中用OU和CN参数扩展初始ldapConnection。有没有办法在不公开Initialize方法的情况下实现这一点
非常感谢 可能是这样的:
public bool AccountExists(string userEmail)
{
using (var root = GetLdapRoot())
{
using (var searcher = new DirectorySearcher(root))
{
searcher.Filter = string.Format("(&(objectClass=User)(mail={0}))", userEmail);
searcher.PropertiesToLoad.Add("email");
var result = searcher.FindAll();
return result.Count > 0;
}
}
}
private static DirectoryEntry GetLdapRoot()
{
return new DirectoryEntry("LDAP://DC=com"); //or whatever your root domain is. Set credentials if you need to
}
通过设置一个过滤器并指定要加载的属性,搜索将更加高效。通过使用根目录作为LDAP://字符串,您应该搜索整个目录。可能是这样的:
public bool AccountExists(string userEmail)
{
using (var root = GetLdapRoot())
{
using (var searcher = new DirectorySearcher(root))
{
searcher.Filter = string.Format("(&(objectClass=User)(mail={0}))", userEmail);
searcher.PropertiesToLoad.Add("email");
var result = searcher.FindAll();
return result.Count > 0;
}
}
}
private static DirectoryEntry GetLdapRoot()
{
return new DirectoryEntry("LDAP://DC=com"); //or whatever your root domain is. Set credentials if you need to
}
通过设置一个过滤器并指定要加载的属性,搜索将更加高效。通过使用根目录作为LDAP://字符串,您应该搜索整个目录。如果您只需要搜索基于CN的DirectorySearcher,您可以使用DirectorySearcher进行搜索。如果您只需要搜索基于CN的DirectorySearcher,您可以使用DirectorySearcher进行搜索。