我可以很好地添加一个用户,但是我不能将它添加到本地组。我收到此错误:-
无法将成员添加到本地组或从本地组中删除,因为该成员不存在。
这是我正在使用的代码。我做错了什么?这只是本地机器,我绝对有权这样做,并且该组肯定存在。
try
{
using (DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://" + serverName))
{
DirectoryEntries entries = hostMachineDirectory.Children;
foreach (DirectoryEntry entry in entries)
{
if (entry.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
{
// Update password
entry.Invoke("SetPassword", password);
entry.CommitChanges();
return true;
}
}
DirectoryEntry obUser = entries.Add(userName, "User");
obUser.Properties["FullName"].Add("Used to allow users to login to Horizon. User created programmatically.");
obUser.Invoke("SetPassword", password);
obUser.Invoke("Put", new object[] {
"UserFlags",
0x10000
});
obUser.CommitChanges();
foreach (string group in groups)
{
DirectoryEntry grp = hostMachineDirectory.Children.Find(group, "group");
if (grp != null) { grp.Invoke("Add", new object[] { obUser.Path.ToString() }); }
}
return true;
}
}
catch (Exception ex)
{
returnMessage = ex.InnerException.Message;
return false;
}