第一个类,获取AD实例类;
AdHerlp.cs
public static class AdHerlp
{
#region 创建AD连接
/// <summary>
/// 创建AD连接
/// </summary>
/// <returns></returns>
public static DirectoryEntry GetDirectoryEntry()
{
DirectoryEntry de = new DirectoryEntry();
de.Path = "http://www.cnblogs.com/IsNull/admin/ldap://qjyczsgl/CN=Users,DC=qjyczsgl,DC=com";
de.Username = @"qjyczsgl\zsgl";
de.Password = "qjyczsgl";
return de;
}
#endregion
#region 获取目录实体集合
/// <summary>
///
/// </summary>
/// <param name="DomainReference"></param>
/// <returns></returns>
public static DirectoryEntry GetDirectoryEntry(string DomainReference)
{
DirectoryEntry entry = new DirectoryEntry("http://www.cnblogs.com/IsNull/admin/ldap://qjyczsgl/" + DomainReference, "zsgl", "qjyczsgl", AuthenticationTypes.Secure);
return entry;
}
#endregion
}
AD操作类
myDirectory.cs
class myDirectory
{
/// <summary>
/// 判断用户是否存在
/// </summary>
/// <param name="UserName"></param>
/// <returns></returns>
public bool UserExists(string UserName)
{
DirectoryEntry de = AdHerlp.GetDirectoryEntry();
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(&(objectClass=user) (cn=" + UserName + "))";
SearchResultCollection results = deSearch.FindAll();
if (results.Count == 0)
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// 修改用户属性
/// </summary>
/// <param name="de"></param>