Creating a user in the active directory

I am going to create a web page to create a user in the active directory.

To create a user account, I use this method:

public string CreateUserAccount(string ldapPath, string userName, 
    string userPassword)
{
    try
    {
        string oGUID = string.Empty;
        string connectionPrefix = "LDAP://" + ldapPath;
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
        DirectoryEntry newUser = dirEntry.Children.Add
            ("CN=" + userName, "user");
        newUser.Properties["samAccountName"].Value = userName;
        newUser.CommitChanges();
        oGUID = newUser.Guid.ToString();

        newUser.Invoke("SetPassword", new object[] { userPassword });
        newUser.CommitChanges();
        dirEntry.Close();
        newUser.Close();
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        //DoSomethingwith --> E.Message.ToString();

    }
    return oGUID;
}

An error occurred while executing this method:

"Server is down"

+3
source share
2 answers

let's say we have an active directory installed with a domain TestDomain.com, and you have a unit (organizational unit) called USERS, and you have a user in it calledTestUser

so we can say the following

ldapDomain: TestDomain.com dc = contoso, dc = com
objectPath: : CN = TestUser, OU = USERS, DC = TestDomain, DC = com
userDn: : CN = TestUser, OU = USERS, DC = TestDomain, DC = com

, , ( ldap)

:

string ldapPath = "LDAP://OU=USERS, DC=TestDomain, DC=com"

:
http://www.selfadsi.org/ldap-path.htm
http://www.informit.com/articles/article.aspx?p=101405&seqNum=7
http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.path.aspx

+3

System.DirectoryServices

To use this namespace you need to add reference  System.DirectoryServices.dll 

       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)
            {

            }
        }

System.DirectoryServices.AccountManagement

 To use this namespace you need to add reference  System.DirectoryServices.AccountManagement.dll 

              PrincipalContext ouContex = new PrincipalContext(ContextType.Domain, "TestDomain.local",           "OU=TestOU,DC=TestDomain,DC=local");

        for (int i = 0; i < 3; i++)
        {
            try
            {
                UserPrincipal up = new UserPrincipal(ouContex);
                up.SamAccountName = "TestUser" + i;
                up.SetPassword("password");
                up.Enabled = true;
                up.ExpirePasswordNow();
                up.Save();
            }
            catch (Exception ex)
            {

            }
        }
+1

Source: https://habr.com/ru/post/1756507/


All Articles