The specified network password is not correct on creating user

0

I ma trying to create user on local system using following code:

            DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://localhost");
            DirectoryEntries entries = hostMachineDirectory.Children;
            foreach (DirectoryEntry entry in entries)
            {
                if (entry.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
                    throw new Exception("User already exists");
            }

            DirectoryEntry user = entries.Add(userName, "user");
            user.Properties["FullName"].Add("Local user");
            user.Properties["Description"].Add(description);
            user.Invoke("SetPassword", new object[] { password } );

            //# ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD
            user.Invoke("Put", new object[] { "UserFlags", 0x0040 + 0x10000 });
            user.CommitChanges();

Unfortunately every time I receive

System.Runtime.InteropServices.COMException (0x80070056): The specified network password is not correct.

   at System.DirectoryServices.DirectoryEntry.CommitChanges()
   at ...SystemUsers.Create(String userName, String password, String description) in c:\...\SystemUsers.cs:line 36

error. I check all security settings. This is not related to password complexity. When password is to short or not complex enough then I receive other error. Computer is connected to domain.

.net
wpf
windows-server-2012
asked on Stack Overflow Aug 14, 2014 by szaman

1 Answer

0

I finally found a solution.

In this line

DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://localhost");

I had to replace localhost with local machine name. And that's it.

answered on Stack Overflow Aug 18, 2014 by szaman

User contributions licensed under CC BY-SA 3.0