如何以编程方式创建的Windows 7或Windows Server 2008的Windows用户帐户? [英] How to programmatically create Windows user accounts on Windows 7 or Windows Server 2008?
问题描述
我一直在试图创建Windows 7计算机上新的本地用户帐户。我用了System.DirectoryServices.DirectoryEntry类(如这里),但它似乎不工作。
I've been trying to create new local user accounts on windows 7 machine. I used the System.DirectoryServices.DirectoryEntry class (as in here) but it doesn't seem to work.
下面的文章中的代码:
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,用户);
我收到了
System.Runtime.InteropServices.COMException
与 {未知错误(0x80005000)}
作为异常消息,和 -2147463168
的错误代码。
as the exception message, and -2147463168
as the error code.
我想这大概是因为本文针对Windows XP和下面的机器,我针对Windows 7和在Windows Server 2008
I assume this is probably because the article targets Windows XP and below machines, and I'm targeting windows 7 and Windows server 2008.
任何帮助表示赞赏!
更新:
的对于某些神秘的原因,我不再看到, System.Runtime.InteropServices.COMException
,但是,在这里提交修改时, newuser.CommitChanges()
,我得到一个 UnAuthorizedAccessException
。我试图运行应用程序作为管理员,但仍无法正常工作的
Any help appreciated!
Update:
For some mysterious reason, i'm no longer seeing that System.Runtime.InteropServices.COMException
, however, when committing the changes here newuser.CommitChanges()
, I get a "UnAuthorizedAccessException
". I tried running the app as administrator, but still not working.
更新2:
OK,更改为UserPrincipal下课后,我得到了follwoing代码工作:
Update 2:
OK, after changing to the UserPrincipal class, i got the follwoing code to work:
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);
}
}
运行此代码还有当我运行它作为一个控制台应用程序 - 当然作为运行系统管理员用户,但是当我部署它作为一个窗口服务,用安全帐户设置为本地系统,我得到一个 InvlaidOperationException
说底层存储不支持此属性
This code runs well when I run it as a console app -of course run as administrator- but when i deployed it as a windows service, with the security account set as "LocalSystem", i get an InvlaidOperationException
saying "The underlying store does not support this property"
思考?
推荐答案
OK,如果你检查我的最后一次更新,下面的代码片断工作:
OK, if you check my last update, the following snippet worked:
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安全会让它运行。这样做了,现在一切都很酷!
That worked as a console app, but failed to execute due to security exceptions when deployed as a windows service. A solution is to trust that assembly (the windows service assembly) so that the .net security will let it run. That's done, now everything is cool!
这篇关于如何以编程方式创建的Windows 7或Windows Server 2008的Windows用户帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!