2011-07-27 113 views
10

我在哪里可以找到执行以下操作的示例?Active Directory中的用户/组权限

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

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

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

+0

什么语言? – TheGeekYouNeed

+0

我在.NET/C#中使用Active Directory API。 – user802165

回答

13

如果您使用.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()方法上FileInfoDirectoryInfo类检索:

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

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

解密及那些决策意识是一个整体完全不同的故事!

+0

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

相关问题