【问题标题】:User/Group Permissions in Active DirectoryActive Directory 中的用户/组权限
【发布时间】:2011-10-14 14:28:08
【问题描述】:

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

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

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

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

【问题讨论】:

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

标签: c# permissions active-directory


【解决方案1】:

如果您使用的是 .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(访问控制列表)中 - 但从用户或组,没有机制可以获取所有它可能在机器/服务器的任何地方拥有权限。

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

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

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

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

【讨论】:

  • 这是我的怀疑。感谢您的帮助。
猜你喜欢
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
相关资源
最近更新 更多