操作AD域net
.net 操作AD 域(转)上2011-06-14 16:21using System;using System.Collections.Generic;using System.Linq;using
.net 操作AD 域(转)上
2011-06-14 16:21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.DirectoryServices;
namespace OperateADLibrary
{
public class OperateAD
{
///
/// 域名
///
private string _domain;
///
/// 主机域IP
///
private string _domainIp;
///
/// 管理员账号
///
private string adminUser;
///
/// 管理员密码
///
private string adminPwd;
///
/// 路径的最前端
///
private string _ldapIdentity;
///
/// 路径的最后端
///
private string _suffixPath;
#region 构造函数
///
/// 构造函数
/// 从webConfig 的AppSettings 属性读取值初始化字段 ///
public OperateAD(string domain, string domainIp, string adUser, string adPwd)
,{
//_domain =
System.Configuration.ConfigurationManager.AppSettings["Domain"].ToString();
//_domainIp =
System.Configuration.ConfigurationManager.AppSettings["DomainIp"].ToString();
//adminUser =
System.Configuration.ConfigurationManager.AppSettings["ADAdminUser"].ToString();
//adminPwd =
System.Configuration.ConfigurationManager.AppSettings["ADAdminPassword"].ToString(); //_ldapIdentity = "LDAP://" _domainIp "/";
//_suffixPath = "DC=" _domain ",DC=COM";
//_domain = "bdxy";
//_domainIp = "10.1.209.197";
//adminUser = "administrator";
//adminPwd = "123456";
_domain = domain;
_domainIp = domainIp;
adminUser = adUser;
adminPwd = adPwd;
_ldapIdentity = "LDAP://" _domainIp "/";
_suffixPath = "DC=" _domain ",DC=com";
}
#endregion
#region 组织结构下添加AD 账户
///
/// 添加AD 账户
///
/// 组织名称
/// 域账户
///
public bool AddADAccount(string organizeName, DomainUser user)
{
DirectoryEntry entry = null;
try
{
if (ExitOU(organizeName) && user != null)
{
entry = new DirectoryEntry(GetOrganizeNamePath(organizeName), adminUser, adminPwd, AuthenticationTypes.Secure);
//增加账户到域中
DirectoryEntry NewUser = entry.Children.Add("CN=" user.UserName, "user"); NewUser.Properties["sAMAccountName"].Add(user.UserName); //account
,NewUser.Properties["userPrincipalName"].Value = user.UserPrincipalName; //user logon name,xxx@bdxy.com
NewUser.Properties["givenName"].Value = "New User";//名
NewUser.Properties["initials"].Value = "Ms";
NewUser.Properties["name"].Value = "12";//full name
NewUser.Properties["sn"].Value = user.UserId;
NewUser.Properties["displayName"].Value = user.UserName;
NewUser.Properties["company"].Value = "1234";
NewUser.Properties["physicalDeliveryOfficeName"].Value =
user.PhysicalDeliveryOfficeName;
NewUser.Properties["Department"].Value = user.Department;
if (user.Telephone != null && user.Telephone != "")
{
NewUser.Properties["telephoneNumber"].Value = user.Telephone; }
if (user.Email != null && user.Email != "")
{
NewUser.Properties["mail"].Value = user.Email;
}
if (user.Description != null && user.Description != "")
{
NewUser.Properties["description"].Value = user.Description; }
NewUser.CommitChanges();
//设置密码
//反射调用修改密码的方法(注意端口号的问题 端口号会引起方法调用异常) NewUser.Invoke("SetPassword", new object[] { user.UserPwd }); //默认设置新增账户启用
NewUser.Properties["userAccountControl"].Value = 0x200;
NewUser.CommitChanges();
//DomainUser._success = "账户添加成功!";
return true;
}
else
{
//DomainUser._failed = "在域中不存在直属组织单位";
return false;
}
}
,catch (System.DirectoryServices.DirectoryServicesCOMException ex) {
//DomainUser._failed = "账户添加失败!" ex.Message.ToString(); return false;
}
finally
{
if (entry != null)
{
entry.Dispose();
}
}
}
#endregion
#region 重命名账户
///
/// 重命名账户
///
/// 管理员名称
/// 管理员密码
/// 原用户名
/// 新用户名
public bool RenameUser(string oldUserName, string newUserName) {
try
{
DirectoryEntry userEntry = FindObject("user", oldUserName); if (userEntry != null)
{
userEntry.Rename("CN=" newUserName);
userEntry.CommitChanges();
//DomainUser._success = "重命名成功!";
return true;
}
//DomainUser._failed = "没找到用户!" oldUserName; return false;
}
catch (Exception ex)
{
//DomainUser._failed = "重命名失败!" ex.Message.ToString(); return false;
}
}
,#endregion
#region 设置用户密码
///
/// 设置用户密码
///
/// 用户名
/// 密码
public bool SetUserPassword(string userName, string password)
{
try
{
DirectoryEntry userEntry = FindObject("user", userName);
if (userEntry != null)
{
userEntry.Invoke("SetPassword", new object[] { password }); userEntry.CommitChanges();
//DomainUser._success = "密码设置成功!";
return true;
}
//DomainUser._failed = "没找到用户!" userName;
return false;
}
catch (Exception ex)
{
//DomainUser._failed = "密码设置失败!" ex.Message.ToString(); return false;
}
}
#endregion
#region 修改密码
///
/// 修改密码
///
/// 用户
/// 旧密码
/// 新密码
public bool ChangePassword(string username, string oldpwd, string newpwd) {
try
{
DirectoryEntry entry = FindObject("user", username);
if (entry != null)
,{
// to-do: 需要解决密码策略问题
entry.Invoke("ChangePassword", new object[] {oldpwd, newpwd }); entry.CommitChanges();
entry.Close();
// DomainUser._success = "密码修改成功!";
return true;
}
else
{
// DomainUser._failed = "没找到用户!" username;
return false;
}
}
catch (Exception ex)
{
//DomainUser._failed = "密码修改失败!" ex.Message.ToString();
return false;
}
}
#endregion
#region 删除账户
///
/// 删除AD 账户,使用当前上下文的安全信息
///
/// 用户名称
public bool DeleteADAccount(string userName)
{
try
{
DirectoryEntry user = FindObject("user", userName);
if (user != null)
{
using (DirectoryEntry de = new DirectoryEntry(user.Parent.Path, adminUser, adminPwd))
{
de.Children.Remove(user);
de.CommitChanges();
//DomainUser._success = "账户删除成功!";
return true;
}
}
,// DomainUser._failed = "未找到账户!";
return false;
}
catch (Exception ex)
{
//DomainUser._failed = "账户删除失败!" ex.Message.ToString(); return false;
}
}
#endregion
.net 操作AD 域(转) 中
2011-06-14 16:22
#region 创建OU
///
/// 创建OU
///
/// 管理员名称
/// 管理员密码
/// 创建的OU 名称
/// 父组织单位
///
public DirectoryEntry CreateOrganizeUnit(string name, string parentOrganizeUnit) {
DirectoryEntry parentEntry = null;
try
{
//示例顶级" LDAP://10.1.209.197/dc=bdxy,dc=com"
parentEntry = new DirectoryEntry(GetOrganizeNamePath(parentOrganizeUnit), adminUser, adminPwd,
AuthenticationTypes.Secure);
DirectoryEntry organizeEntry = parentEntry.Children.Add("OU=" name, "organizationalUnit");
organizeEntry.CommitChanges();
//DomainUser._success = "组织单位添加成功!";
return organizeEntry;
}
catch (System.DirectoryServices.DirectoryServicesCOMException ex)
{
//DomainUser._failed = "添加组织单位失败!" ex.Message.ToString(); return new DirectoryEntry();
}
finally
{
if (parentEntry != null)
,{
parentEntry.Dispose();
}
}
}
#endregion
#region 删除OU
///
/// 删除OU
///
/// 创建的OU 名称
/// 父组织单位
///
public bool DeleteOrganizeUnit(string name, string parentOrganizeUnit)
{
DirectoryEntry parentEntry = null;
try
{
//示例顶级" LDAP://10.1.209.197/dc=bdxy,dc=com"
parentEntry = new DirectoryEntry(GetOrganizeNamePath(parentOrganizeUnit), adminUser, adminPwd,
AuthenticationTypes.Secure);
DirectoryEntry organizeEntry = parentEntry.Children.Find("OU=" name, "organizationalUnit");
//先删除组织单元下的用户或者组
parentEntry.Children.Remove(organizeEntry);
organizeEntry.CommitChanges();
//DomainUser._success = "组织单位删除成功!";
return true;
}
catch (System.DirectoryServices.DirectoryServicesCOMException ex)
{
//DomainUser._failed = "组织单位删除失败!" ex.Message.ToString(); return false;
}
finally
{
if (parentEntry != null)
{
parentEntry.Dispose();
}
}
}
,#endregion
#region 创建组
///
/// 创建组
///
/// 组名
/// 组织单位
///
public bool CreateGroup(string name, string OrganizeUnit)
{
DirectoryEntry parentEntry = null;
try
{
parentEntry = new DirectoryEntry(GetOrganizeNamePath(OrganizeUnit), adminUser, adminPwd,
AuthenticationTypes.Secure);
DirectoryEntry groupEntry = parentEntry.Children.Add("CN=" name, "group"); groupEntry.CommitChanges();
// DomainUser._success = "组创建成功!";
return true;
}
catch (System.DirectoryServices.DirectoryServicesCOMException ex)
{
//DomainUser._failed = "组创建失败!" ex.Message.ToString();
return false;
}
finally
{
if (parentEntry != null)
{
parentEntry.Dispose();
}
}
}
#endregion
#region 删除组
///
/// 删除组
///
/// 组名
/// 组织单位
,///
public bool DeleteGroup(string name, string OrganizeUnit)
{
DirectoryEntry parentEntry = null;
try
{
parentEntry = new DirectoryEntry(GetOrganizeNamePath(OrganizeUnit), adminUser, adminPwd,
AuthenticationTypes.Secure);
DirectoryEntry groupEntry = parentEntry.Children.Find("CN=" name, "group"); parentEntry.Children.Remove(groupEntry);
groupEntry.CommitChanges();
//DomainUser._success = "组删除成功!";
return true;
}
catch (System.DirectoryServices.DirectoryServicesCOMException ex)
{
// DomainUser._failed = "组删除失败!" ex.Message.ToString();
return false;
}
finally
{
if (parentEntry != null)
{
parentEntry.Dispose();
}
}
}
#endregion
#region 将用户加入到用户组中
///
/// 将用户加入到用户组中
///
/// 用户名
/// 组织名
/// 组名
/// 组所在路径
///
DirectoryEntry group = null;