【问题标题】:How to programmatically create Windows user accounts on Windows 7 or Windows Server 2008?如何在 Windows 7 或 Windows Server 2008 上以编程方式创建 Windows 用户帐户?
【发布时间】:2011-05-12 21:19:07
【问题描述】:

我一直在尝试在 Windows 7 机器上创建新的本地用户帐户。我使用了 System.DirectoryServices.DirectoryEntry 类(如here),但它似乎不起作用。

这是文章中的代码:

static void Main(string[] args)
{
try
    {
 DirectoryEntry AD = new DirectoryEntry("WinNT://" + 
                     Environment.MachineName + ",computer");
 DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user");
 NewUser.Invoke("SetPassword", new object[] {"#12345Abc"});
 NewUser.Invoke("Put", new object[] {"Description", "Test User from .NET"});
 NewUser.CommitChanges();
 DirectoryEntry grp;

 grp = AD.Children.Find("Guests", "group");
 if (grp != null) {grp.Invoke("Add", new object[] {NewUser.Path.ToString()});}
 Console.WriteLine("Account Created Successfully");
 Console.ReadLine();
}
catch (Exception ex)
{
 Console.WriteLine(ex.Message);
 Console.ReadLine();

}
}

当执行这一行时

DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user");

我得到一个

System.Runtime.InteropServices.COMException 与“{"Unknown error (0x80005000)"}

作为异常消息,-2147463168 作为错误代码。

我认为这可能是因为本文针对的是 Windows XP 及以下机器,而我的目标是 Windows 7 和 Windows Server 2008。

任何帮助表示赞赏!

更新:
由于某种神秘的原因,我不再看到 System.Runtime.InteropServices.COMException,但是,当在这里提交更改 newuser.CommitChanges() 时,我得到了一个“UnAuthorizedAccessException”。我尝试以管理员身份运行该应用程序,但仍然无法运行。

更新 2:
好的,在更改为 UserPrincipal 类后,我得到了以下代码:

public UserPrincipal CreateNewUser(string sUserName, string sPassword)
        {
            // first check that the user doesn't exist
            if (GetUser(sUserName) == null)
            {
                PrincipalContext oPrincipalContext = GetPrincipalContext();

                UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext);
                oUserPrincipal.Name = sUserName;
                oUserPrincipal.SetPassword(sPassword);
                //User Log on Name
                //oUserPrincipal.UserPrincipalName = sUserName;
                oUserPrincipal.Save();

                return oUserPrincipal;
            }

            // if it already exists, return the old user
            return GetUser(sUserName);
        }
    }


当我将它作为控制台应用程序运行时,此代码运行良好 - 当然以管理员身份运行 - 但是当我将其部署为 Windows 服务时,安全帐户设置为“LocalSystem”,我得到一个 InvlaidOperationException 说“底层存储不支持此属性”

想法?

【问题讨论】:

  • 您是否运行了提升的应用程序?
  • 您的意思是“作为管理员”?是的。同样的错误。
  • 该错误实际上意味着“指定的目录服务属性或值不存在”。这种解释是否有帮助是另一回事。
  • Update 2 对没有丢失代码的旅友没有多大用处,我认为它可能来自这里 stackoverflow.com/questions/4199667/…

标签: c# windows-7


【解决方案1】:

好的,如果您检查我的上次更新,则以下 sn-p 有效:

public UserPrincipal CreateNewUser(string sUserName, string sPassword)
        {
            // first check that the user doesn't exist
            if (GetUser(sUserName) == null)
            {
                PrincipalContext oPrincipalContext = GetPrincipalContext();

                UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext);
                oUserPrincipal.Name = sUserName;
                oUserPrincipal.SetPassword(sPassword);
                //User Log on Name
                //oUserPrincipal.UserPrincipalName = sUserName;
                oUserPrincipal.Save();

                return oUserPrincipal;
            }

            // if it already exists, return the old user
            return GetUser(sUserName);
        }
    }

它作为控制台应用程序运行,但在部署为 Windows 服务时由于安全异常而无法执行。一种解决方案是信任该程序集(Windows 服务程序集),以便 .net 安全性让它运行。大功告成,现在一切都很酷!

【讨论】:

  • Galilyou,您能否详细说明一下您是如何在 Windows 服务中实现此功能的?
  • GetUser 的定义在哪里?
  • 感谢 Stephen Kennedy,我在这里找到了 GetUser:stackoverflow.com/questions/4199667/…
  • 别忘了处理 oPrincipalContext 和 oUserPrincipal
【解决方案2】:

您需要在您的用户名前加上 CN=,如下所示:

DirectoryEntry NewUser = AD.Children.Add("CN=TestUser1", "user");

【讨论】:

  • 感谢 Warren,但这似乎并没有多大帮助。
猜你喜欢
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 2010-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多