Create Active Directory user in .NET (C#)

前端 未结 3 1939
情深已故
情深已故 2021-02-01 06:08

I need to create a new user in Active Directory. I have found several examples like the following:

using System;
using System.DirectoryServices;

namespace test          


        
3条回答
  •  一向
    一向 (楼主)
    2021-02-01 06:49

    Assuming your OU path OU=x,DC=y,DC=com really exists - it should work :-)

    Things to check:

    • you're adding a value to the "samAccountName" - why don't you just set its value:

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

    Otherwise you might end up with several samAccountNames - and that won't work.....

    • you're not setting the userAccountControl property to anything - try using:

       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

    • 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.

    Marc

提交回复
热议问题