且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建一个带有密码在C#中的Active Directory用户

更新时间:2023-08-21 14:28:22

您可以现在就做这整个过程变得更加容易与System.DirectoryServices.AccountManagement(只要你在净3.5):

You can do this whole process much easier now with System.DirectoryServices.AccountManagement (long as you're on .Net 3.5):

在这里看到一个完整的破败

这是你的具体情况一个简单的例子:

Here's a quick example of your specific case:

using(var pc = new PrincipalContext(ContextType.Domain))
{
  using(var up = new UserPrincipal(pc))
  {
    up.SamAccountName = username;
    up.EmailAddress = email;
    up.SetPassword(password);
    up.Enabled = true;
    up.ExpirePasswordNow();
    up.Save();
  }
}