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.
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.
User contributions licensed under CC BY-SA 3.0