如何在Windows 7或Windows Server 2008上以编程方式创建Windows用户帐户?

7

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

以下是文章中的代码:

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”时,我收到一个InvalidOperationException异常,显示“基础存储不支持此属性”。 有什么想法?

你以管理员身份运行了应用程序吗? - Kate Gregory
你的意思是“以管理员身份运行”吗?是的。同样的错误。 - Ahmed
该错误实际上意味着“指定的目录服务属性或值不存在”。这个解释是否有帮助是另一回事。 - Gabe
3
没有缺失的代码,旅行者们对更新2没什么用处。我认为缺失的代码可能来自这里:http://stackoverflow.com/questions/4199667/finding-a-user-through-the-groupprincipal - Stephen Kennedy
2个回答

8

好的,如果你检查我的最后一个更新,以下代码片段可以工作:

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安全性能够允许其运行。现在已经完成了这一步骤,一切都很顺利!


1
Galilyou,请问你能详细说明一下你是如何在Windows服务中使其正常工作的吗? - Stephen Kennedy
GetUser 的定义在哪里? - Millie Smith
1
感谢Stephen Kennedy,我在这里找到了GetUser:http://stackoverflow.com/questions/4199667/finding-a-user-through-the-groupprincipal - Millie Smith
不要忘记释放 oPrincipalContext 和 oUserPrincipal。 - scegg

0

您需要在用户名前加上CN=,像这样:

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


感谢您的回复,但这似乎并没有太大帮助。 - Ahmed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接