创建用户时指定的网络密码不正确

时间:2014-08-14 10:54:13

标签: .net wpf windows-server-2012

我尝试使用以下代码在本地系统上创建用户:

            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

错误。我检查所有安全设置。这与密码复杂性无关。当密码短或不够复杂时,我收到其他错误。计算机已连接到域。

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。

在这一行

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

我不得不用本地机器名替换localhost。就是这样。