.net 创建用户时指定的网络密码不正确
我正在尝试使用以下代码在本地系统上创建用户:.net 创建用户时指定的网络密码不正确,.net,wpf,windows-server-2012,.net,Wpf,Windows Server 2012,我正在尝试使用以下代码在本地系统上创建用户: DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://localhost"); DirectoryEntries entries = hostMachineDirectory.Children; foreach (DirectoryEntry entry in entries)
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();
不幸的是每次我收到
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
错误。我检查所有的安全设置。这与密码的复杂性无关。当密码太短或不够复杂时,我会收到其他错误。计算机已连接到域。我终于找到了解决方案 在这一行
DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://localhost");
我不得不用本地机器名替换localhost。就这样