使用C#访问 Active Directory域服务,需引用System.DirectoryServices命名空间,该命名空间包含两个组件类,DirectoryEntry和 DirectorySearcher。
DirectoryEntry类可封装 ActiveDirectory域服务层次结构中的节点或对象,使用此类绑定到对象、读取属性和更新特性;
使用DirectorySearcher类可对 Active Directory域服务层次结构执行查询。
如果仅要对AD的组织单元,群组和用户进行查询或者更改属性,使用上述两个类即可。
下面给出操作AD域的部分代码:
1、连接到域
DirectoryEntry objDE = new DirectoryEntry(ADname, Loginname, Loginpwd);
ADname :是域名,一般格式是:"LDAP://****";****为域的名字(一般是大写的英文字符串)
Loginname: 登录域的用户名(保存在域中的名字,一般是中文名字的拼音)
Loginpwd: 用户名对应的登录密码
2、查询域中的所有用户
DirectoryEntry objDE = new DirectoryEntry(ADtxt, Logintxt, Loginpwdtxt);
string strFilter = "(&(objectCategory=person)(objectClass=user))";
DirectorySearcher objSearcher = new Directo