locked
Add new user to Active Directory RRS feed

  • Question

  • User-261222641 posted

    Hello 

    i've Web Application using MVC 5.

    i would like to add user to Active Directory on Form Submitted.

    i am using the following code 

     try
                    {
                        int NORMAL_ACCOUNT = 0x200;
                        int PWD_NOTREQD = 0x20; 
                        string ldapPath= "OU=dc1,DC=MYDOMAIN,DC=LOCAL";
                        string userName = model.Email; 
                        string connectionPrefix = "LDAP://" + ldapPath;
                        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
                        DirectoryEntry newUser = dirEntry.Children.Add("CN=" + userName, "user");
                        newUser.Properties["sAMAccountName"].Value = userName;
                        newUser.Properties["userAccountControl"].Value = NORMAL_ACCOUNT | PWD_NOTREQD;
                        newUser.CommitChanges();
    
                        newUser.Invoke("SetPassword", new object[] { model.Password });
                        newUser.CommitChanges();
                        dirEntry.Close();
                        newUser.Close();
                        return RedirectToAction("Index", "Home");
                    }
                    catch (System.DirectoryServices.DirectoryServicesCOMException Ex)
                    {
                        Console.WriteLine(Ex.Message);
    
                    }

    When I run this code I get no errors, but no new user is created.

    i've sufficient privileges on the computer.

    Thanks in advance for your help.

    Monday, February 8, 2016 6:11 PM

All replies

  • User587237524 posted

    Hi Cpgtech,

    From your description, I cannot point out the root cause. So what we need is to narrow down the issue and find more about it.

    The following page describes some common scenarios about how to use System.DirectoryServices namespace in ASP.NET project, please refer to see if it could solve your issue.

    https://support.microsoft.com/en-us/kb/329986

    Please feel free to let me know your result.

    Regards,

    Wednesday, February 10, 2016 9:59 AM