如何默认将新的 Windows 用户添加到 Users 组
我有一段创建 Windows 用户的代码。一切都很好,用户确实被创建了。但是,当我尝试 pinvoke LoadUserProfile 时,操作失败,因为该用户不在 Users 组中。现在,我知道如何务实地将此用户添加到 Users 组,但我不想对组名称“Users”进行硬编码,因为它可能会根据区域设置而改变。有没有办法默认将用户添加到 Users 组(顺便说一下, runas user /add 命令已经做到了这一点)?
DirectoryEntry dirEntry = new DirectoryEntry("WinNT://" + domain);
DirectoryEntries entries = dirEntry.Children;
DirectoryEntry user = entries.Add(username, "User");
user.Properties["FullName"].Add("Dr Zoidberg");
user.Invoke("SetPassword", password);
user.CommitChanges();
I have a piece of code that creates a Windows user. Everything is fine and the user is indeed created. However, when I try to pinvoke LoadUserProfile, the operation fails, because the user is not in the Users group. Now, I know how to pragmatically add this user to the Users group, but I dont want to hard-code group name "Users", since it might change depending on the locale. Is there a way to add the user to the Users group by default (by the way, runas user /add command already does that)?
DirectoryEntry dirEntry = new DirectoryEntry("WinNT://" + domain);
DirectoryEntries entries = dirEntry.Children;
DirectoryEntry user = entries.Add(username, "User");
user.Properties["FullName"].Add("Dr Zoidberg");
user.Invoke("SetPassword", password);
user.CommitChanges();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Windows 和 Active Directory 有许多“众所周知的 SID”,它们是内置帐户和组的安全标识符。您可以使用众所周知的 SID 绑定到 Users 组,因为无论区域设置如何,它都不会更改。管理员甚至可以将用户组重命名为其他名称,但 SID 将保持不变。
System.Security.Principal.WellKnownSidType 中枚举了众所周知的 SID
请参阅 http://msdn.microsoft.com/en-us/library/system.security.principal.wellknownsidtype.aspx 了解更多详细信息。
Windows and Active Directory have a numberof "Well-Known SIDs" which are the security identifiers for builtin accounts and groups. You can use the well-known SID to bind to the Users group because it will not change regardless of the locale. An admin can even rename the Users group to something else, but the SID will remain the same.
The well-known SIDs are enumerated in System.Security.Principal.WellKnownSidType
See http://msdn.microsoft.com/en-us/library/system.security.principal.wellknownsidtype.aspx for more details.