如果您使用的是 .NET 3.5 及更高版本,您应该查看System.DirectoryServices.AccountManagement
(S.DS.AM) 命名空间。在这里阅读所有相关内容:
- 管理 .NET Framework 3.5 中的目录安全主体 http://msdn.microsoft.com/en-us/magazine/cc135979.aspx
- 关于 System.DirectoryServices.AccountManagement 的 MSDN 文档 http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx
基本上,您可以定义域上下文并轻松在 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....
}
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
// if found....
if (group != null)
{
// iterate over members
foreach (Principal p in group.GetMembers())
{
Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
// do whatever you need to do to those members
}
}
新的 S.DS.AM 使 AD 中的用户和组的使用变得非常容易!
最后一点:权限。这些不存储在 Active Directory 中 - 因此,您无法从任何 AD 代码中检索它们。
权限存储在各个文件系统项目上,例如文件和/或目录 - 或其他对象(如注册表项等)。当您拥有 AD 组或用户帐户时,您可以读取它的 SID(安全标识符)属性 - 该 SID 将显示在整个 Windows 的 ACL(访问控制列表)中 - 但是从用户或组中,没有机制可以获取所有信息它可能在计算机/服务器中的任何位置拥有的权限。
文件和目录的权限可以例如可以使用检索.GetAccessControl()
方法上的FileInfo
and DirectoryInfo
课程:
FileInfo info = new FileInfo(@"D:\test.txt");
FileSecurity fs = info.GetAccessControl();
DirectoryInfo dir = new DirectoryInfo(@"D:\test\");
DirectorySecurity ds = dir.GetAccessControl();
破译和理解这些是完全不同的故事!