【问题标题】:Creating Active Directory user with password in C#在 C# 中使用密码创建 Active Directory 用户
【发布时间】:2010-02-21 12:34:59
【问题描述】:

我正在寻找一种方法来创建 Active Directory 用户并设置他们的密码,最好不要授予我的应用程序/服务域管理员权限。

我尝试了以下方法:

DirectoryEntry newUser = _directoryEntry.Children.Add("CN=" + fullname, USER);
newUser.Properties["samAccountName"].Value = username;
newUser.Properties["userPassword"].Value = password;
newUser.Properties["mail"].Value = email;
newUser.CommitChanges();

用户已创建,但似乎从未为用户设置密码。

有没有人知道在创建用户时如何设置用户密码?我知道

.Invoke("SetPassword", new object[] { password })

但这需要我的代码以域管理员权限运行。因为我真的不明白授予我的代码域管理员权限的意义,只是为了设置初始密码(我也允许用户密码重置,但那些在特定用户的上下文中运行),我希望有人有一个聪明的不需要我这样做的解决方案。

提前致谢!

【问题讨论】:

  • 这是我们唯一的选择,我敢打赌你在同一条船上,但为你提供了下面的 3.5 替代方案,以防万一。与之前唯一选择的 DirectoryEntry 样式相比,我有 1/4 的代码,我强烈建议看看新的命名空间。

标签: c# .net active-directory


【解决方案1】:

您现在可以使用 System.DirectoryServices.AccountManagement 更轻松地完成整个过程(只要您使用 .Net 3.5):

See here for a full rundown

以下是您的具体案例的快速示例:

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();
  }
}

【讨论】:

  • 完成后不要忘记处理上下文和原则对象。这最好使用pcup 上的using 语句来完成。
  • @tvanfosson - 很好,即使是快速示例也应该值得复制/粘贴,更新!
【解决方案2】:

我会使用@Nick 的代码(包装在using 语句中,以便正确处理上下文和主体)。至于权限,您至少需要在创建用户的 OU 上拥有足够的权限才能创建和管理对象。我将创建一个特定用户,您的程序将在该用户下运行,并为其提供足够的权限来执行该特定 OU 中所需的任务,仅此而已。

【讨论】:

    【解决方案3】:

    是的,也可以使用下面的代码来创建大量用户

    DirectoryEntry ouEntry = new DirectoryEntry("LDAP://OU=TestOU,DC=TestDomain,DC=local");
    
    for (int i = 0; i < 10; i++)
    {
        try
        {
            DirectoryEntry childEntry = ouEntry.Children.Add("CN=TestUser" + i,  "user");
            childEntry.CommitChanges();
            ouEntry.CommitChanges();
            childEntry.Invoke("SetPassword", new object[] { "password" });
            childEntry.CommitChanges();
        }
        catch (Exception ex)
        {
        }
    }
    

    【讨论】:

      【解决方案4】:

      试试这个代码。

      DirectoryEntry ouEntry = new DirectoryEntry("LDAP://OU=TestOU,DC=TestDomain,DC=local");
      
      for (int i = 0; i < 10; i++)
      {
          try
          {
              DirectoryEntry childEntry = ouEntry.Children.Add("CN=TestUser" + i,  "user");
              childEntry.CommitChanges();
              ouEntry.CommitChanges();
              childEntry.Invoke("SetPassword", new object[] { "password" });
              childEntry.CommitChanges();
          }
          catch (Exception ex)
          {
      
          }
      }
      

      【讨论】:

        【解决方案5】:

        密码的实际属性是unicodePwd,它需要the documentation 中描述的特定格式。但你可以这样做:

        newUser.Properties["unicodePwd"].Value = Encoding.Unicode.GetBytes("\"NewPassword\"");
        

        这样做,您可以一步创建带有密码的用户。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-05
          • 1970-01-01
          • 1970-01-01
          • 2012-02-01
          • 1970-01-01
          • 2023-03-30
          • 2010-09-28
          相关资源
          最近更新 更多