ADSI = Active Directory Service Interfaces - 这是一个与 Active Directory 对话以在 Active Directory(Microsoft 网络的基于网络的 LDAP 目录)中创建用户、组和计算机帐户的 API。
那么您需要在本地机器/服务器上创建本地用户,还是需要在 Active Directory 中创建组??
如果您使用 .NET 3.5 及更高版本进行编程,您应该查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:
基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// do something here....
}
// create a group
GroupPrincipal group = new GroupPrincipal(ctx, "Group01");
// set other properties on the group here.....
group.Save();
新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!
更新:很遗憾,新的 S.DS.AM 不适用于本地组 :-( 它仅适用于 Active Directory。
如果您需要创建本地 Windows 组,则需要使用旧的 DirectoryEntry 方法 - 类似于:
// bind to your machine's WinNT:// provider
DirectoryEntry computer = new DirectoryEntry("WinNT://YourMachineNameHere");
// create a new local group on your computer
DirectoryEntry newGroup = computer.Children.Add("NewGroupName", "Group");
// save that group to the local machine
newGroup.CommitChanges();
// refresh the property cache so you can set properties like "Description" or others
newGroup.RefreshCache();
newGroup.Properties["description"].Value = "Description for your group....";
newGroup.CommitChanges();
Richard Mueller 有一个great list of Excel sheets 显示所有可用的属性,包括基于 LDAP 的 Active Directory 对象以及非常有限的 WinNT 属性。