C# 如何在Windows 7或Windows Server 2008上以编程方式创建Windows用户帐户?
我一直在尝试在Windows7计算机上创建新的本地用户帐户。我使用了System.DirectoryServices.DirectoryEntry类(如中所示),但它似乎不起作用 以下是文章中的代码:C# 如何在Windows 7或Windows Server 2008上以编程方式创建Windows用户帐户?,c#,windows-7,C#,Windows 7,我一直在尝试在Windows7计算机上创建新的本地用户帐户。我使用了System.DirectoryServices.DirectoryEntry类(如中所示),但它似乎不起作用 以下是文章中的代码: static void Main(string[] args) { try { DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",co
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
带有“{”未知错误(0x80005000)}
”
作为异常消息,-2147463168
作为错误代码
我想这可能是因为本文针对的是WindowsXP及以下的机器,而我的目标是Windows7和WindowsServer2008
感谢您的帮助
更新:
出于某种神秘的原因,我不再看到System.Runtime.InteropServices.COMException
,但是,在这里提交更改newuser.CommitChanges()
,我会得到一个“未授权的dAccessException
”。我尝试以管理员身份运行该应用程序,但仍然无法运行
更新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
说明“底层存储不支持此属性”
想法?您需要在用户名前面加上CN=,如下所示:
DirectoryEntry NewUser=AD.Children.Add(“CN=TestUser1”,“user”)代码>好的,如果您检查我的上次更新,则以下代码段有效:
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安全性允许它运行。完成了,现在一切都很酷 你运行应用程序了吗?你是说“作为管理员”?对相同的错误。该错误实际上意味着“指定的目录服务属性或值不存在”。这个解释是否有用是另一回事。更新2对于没有丢失代码的同行来说没有多大用处,我想可能是从这里来的谢谢Warren,但这似乎没有多大帮助。Galilyou,你能详细说明一下你是如何在Windows服务中实现这一点的吗?GetUser的定义在哪里?感谢Stephen Kennedy,我在这里找到了GetUser:别忘了处理oPrincipalContext和UserPrincipal