Active Directory中的用户/组权限

use*_*165 12 c# permissions active-directory

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

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

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

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

mar*_*c_s 15

如果您使用的是.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
   }
}
Run Code Online (Sandbox Code Playgroud)

新的S.DS.AM使得在AD中与用户和群组玩游戏变得非常容易!

最后一点:权限.这些不存储在Active Directory中 - 因此,您无法从任何AD代码中检索它们.

权限存储在各个文件系统项目上,例如文件和/或目录 - 或其他对象(如注册表项等).当您拥有AD组或用户帐户时,您可以读取它的SID(安全标识符)属性 - 该SID将显示在整个Windows的ACL(访问控制列表)中 - 但是从用户或组中,没有机制可以获取所有它可能在机器/服务器中的任何位置具有权限.

例如,可以使用和类.GetAccessControl()上的方法检索文件和目录的权限:FileInfoDirectoryInfo

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

DirectoryInfo dir = new DirectoryInfo(@"D:\test\");
DirectorySecurity ds = dir.GetAccessControl();
Run Code Online (Sandbox Code Playgroud)

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