简体   繁体   English

在.NET中创建Active Directory用户(C#)

[英]Create Active Directory user in .NET (C#)

I need to create a new user in Active Directory. 我需要在Active Directory中创建一个新用户。 I have found several examples like the following: 我找到了几个例子如下:

using System;
using System.DirectoryServices;

namespace test {
   class Program {
      static void Main(string[] args) {
        try {
            string path = "LDAP://OU=x,DC=y,DC=com";
            string username = "johndoe";

            using (DirectoryEntry ou = new DirectoryEntry(path)) {
               DirectoryEntry user = ou.Children.Add("CN=" + username, "user");

               user.Properties["sAMAccountName"].Add(username);

               ou.CommitChanges();
            }
         } 
         catch (Exception exc) {
             Console.WriteLine(exc.Message);
         }
      }
   }
}

When I run this code I get no errors, but no new user is created. 当我运行此代码时,我没有错误,但没有创建新用户。

The account I'm running the test with has sufficient privileges to create a user in the target Organizational Unit. 我正在运行测试的帐户具有足够的权限来在目标组织单位中创建用户。

Am I missing something (possibly some required attribute of the user object)? 我错过了什么(可能是用户对象的一些必需属性)?

Any ideas why the code does not give exceptions? 任何想法为什么代码不给出例外?

EDIT 编辑
The following worked for me: 以下对我有用:

int NORMAL_ACCOUNT = 0x200;
int PWD_NOTREQD = 0x20;
DirectoryEntry user = ou.Children.Add("CN=" + username, "user");
user.Properties["sAMAccountName"].Value = username;
user.Properties["userAccountControl"].Value = NORMAL_ACCOUNT | PWD_NOTREQD;
user.CommitChanges();

So there were actually a couple of problems: 所以实际上有几个问题:

  1. CommitChanges must be called on user (thanks Rob) 必须在user上调用CommitChanges (感谢Rob)
  2. The password policy was preventing the user to be created (thanks Marc) 密码策略阻止创建用户(感谢Marc)

I think you are calling CommitChanges on the wrong DirectoryEntry. 我认为你在错误的DirectoryEntry上调用CommitChanges。 In the MSDN documentation ( http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentries.add.aspx ) it states the following (emphasis added by me) 在MSDN文档( http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentries.add.aspx )中,它声明了以下内容(重点由我添加)

You must call the CommitChanges method on the new entry to make the creation permanent. 您必须在新条目上调用CommitChanges方法才能使创建成为永久性。 When you call this method, you can then set mandatory property values on the new entry. 调用此方法时,可以在新条目上设置强制属性值。 The providers each have different requirements for properties that need to be set before a call to the CommitChanges method is made. 每个提供程序对在调用CommitChanges方法之前需要设置的属性有不同的要求。 If those requirements are not met, the provider might throw an exception. 如果不满足这些要求,提供程序可能会抛出异常。 Check with your provider to determine which properties must be set before committing changes. 请咨询您的提供商,以确定在提交更改之前必须设置哪些属性。

So if you change your code to user.CommitChanges() it should work, if you need to set more properties than just the account name then you should get an exception. 因此,如果您将代码更改为user.CommitChanges(),它应该可以工作,如果您需要设置更多属性而不仅仅是帐户名,那么您应该得到一个例外。

Since you're currently calling CommitChanges() on the OU which hasn't been altered there will be no exceptions. 由于您当前正在调用尚未更改的OU上的CommitChanges(),因此不会有异常。

Assuming your OU path OU=x,DC=y,DC=com really exists - it should work :-) 假设您的OU路径OU=x,DC=y,DC=com确实存在 - 它应该工作:-)

Things to check: 要检查的事项:

  • you're adding a value to the "samAccountName" - why don't you just set its value: 你正在为“samAccountName”添加一个值 - 为什么不设置它的值:

     user.Properties["sAMAccountName"].Value = username; 

Otherwise you might end up with several samAccountNames - and that won't work..... 否则你可能会得到几个samAccountNames - 这将无法正常工作.....

  • you're not setting the userAccountControl property to anything - try using: 你没有将userAccountControl属性设置为任何东西 - 尝试使用:

      user.Properties["userAccountControl"].Value = 512; // normal account 
  • do you have multiple domain controllers in your org? 你的组织中有多个域控制器吗? If you, and you're using this "server-less" binding (not specifying any server in the LDAP path), you could be surprised where the user gets created :-) and it'll take several minutes up to half an hour to synchronize across the whole network 如果您,并且您正在使用此“无服务器”绑定(未在LDAP路径中指定任何服务器),您可能会惊讶于创建用户的位置:-)并且它将花费几分钟到半小时在整个网络中同步

  • do you have a strict password policy in place? 你有严格的密码政策吗? Maybe that's the problem. 也许这就是问题所在。 I recall we used to have to create the user with the "doesn't require password" option first, do a first .CommitChanges(), then create a powerful enough password, set it on the user, and remove that user option. 我记得我们以前必须首先使用“不需要密码”选项创建用户,首先执行.CommitChanges(),然后创建足够强大的密码,在用户上设置它,并删除该用户选项。

Marc

Check the below code 检查以下代码

 DirectoryEntry ouEntry = new DirectoryEntry("LDAP://OU=TestOU,DC=TestDomain,DC=local");

        for (int i = 3; i < 6; 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)
            {

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM