应用动态目录修改域用户
/// <summary>
/// 创建用户
/// </summary>
/// <param name="Username"></param>
/// <param name="Userpassword"></param>
/// <param name="Path"></param>
/// <returns></returns>
public bool CreateNTUser(string Username, string Userpassword, string Path)
{
DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://WORKGROUP/" + this.txt_ip.Text, this.txt_UserName.Text, this.txt_Password.Text);
DirectoryEntry obUser = obDirEntry.Children.Add(Username, "User"); //增加用户名
obUser.Properties["FullName"].Add(Username); //用户全称
obUser.Invoke("SetPassword", Userpassword); //用户密码
obUser.Invoke("Put", "Description", "尹皓测试用");//用户详细描述
//obUser.Invoke("Put","PasswordExpired",1); //用户下次登录需更改密码
obUser.Invoke("Put", "UserFlags", 66049); //密码永不过期
obUser.Invoke("Put", "HomeDirectory", Path); //主文件夹路径
obUser.CommitChanges();//保存用户
DirectoryEntry dirDomain = new DirectoryEntry("WinNT://" + this.txt_ip.Text, this.txt_UserName.Text, this.txt_Password.Text);
DirectoryEntry user = obDirEntry.Children.Find(Username, "User");
if (obUser != null)
{
DirectoryEntry grp = obDirEntry.Children.Find("administrators", "group");
if (grp != null)
{
//grp.Properties["member"].Add(obUser.Properties["distinguishedName"].Value);
grp.Properties["member"].Add("yinhao");
//grp.Invoke("Add", new object[] { obUser.Path.ToString() });
grp.CommitChanges();
}
}
return true;
}
catch(Exception ex )
{
MessageBox.Show(ex.Message);
return false;
}
}
//删除NT用户
//传入参数:Username用户名
public bool DelNTUser(string Username)
{
try
{
DirectoryEntry obComputer = new DirectoryEntry("WinNT://WORKGROUP/" + txt_ip.Text, this.txt_UserName.Text, this.txt_Password.Text);//获得计算机实例
DirectoryEntry obUser = obComputer.Children.Find(Username, "User");//找得用户
obComputer.Children.Remove(obUser);//删除用户
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
//修改NT用户密码
//传入参数:Username用户名,Userpassword用户新密码
public bool InitNTPwd(string Username, string Userpassword)
{
try
{
DirectoryEntry obComputer = new DirectoryEntry("WinNT://WORKGROUP/" + txt_ip.Text, this.txt_UserName.Text, this.txt_Password.Text);
DirectoryEntry obUser = obComputer.Children.Find(Username, "User");
obUser.Invoke("SetPassword", Userpassword);
obUser.CommitChanges();
obUser.Close();
obComputer.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}