问题 Active Directory中的用户/组权限


我在哪里可以找到执行以下操作的示例?

  1. 从Active Directory中提取用户。
  2. 获取用户所属的组。
  3. 获取分配给每个组的权限列表。

这似乎是一个简单的任务,但我找不到解决方案。

总体目标是分配自定义权限并使用它们来控制应用程序中的权限。


7929
2017-07-27 20:09


起源

什么语言?? - TheGeekYouNeed
我在.NET / C#中使用Active Directory API。 - user802165


答案:


如果您使用的是.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....     
}

// 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 和 DirectoryInfo 类别:

FileInfo info = new FileInfo(@"D:\test.txt");
FileSecurity fs = info.GetAccessControl();

DirectoryInfo dir = new DirectoryInfo(@"D:\test\");
DirectorySecurity ds = dir.GetAccessControl();

解读和理解这些完全是一个完全不同的故事!


14
2017-07-27 20:43



这是我的怀疑。感谢您的帮助。 - user802165