通过 GroupPrincipal 查找用户

2024-06-30

在我的 Active Directory (my.domain) 中,我有许多组(UserGrp1、UserGrp2 等),其中有许多用户。一个用户可以存在于多个组中。我目前的代码允许我使用 GroupPrincipal 类来查找组,然后从那里获取该组的所有成员(请参阅下面的代码)。然而,我真正需要的是找到用户所属的所有组。例如,我有一个名为 Joe Test (sAMAccountName=JOETEST) 的域用户,我需要查找他所属的所有组。做这个的最好方式是什么?

如果我循环访问 GetMembers() 方法返回的所有成员,我可以确定用户是否属于某个组(如下所示),但这对我来说似乎效率低下,如果没有更有效的方法,我会感到惊讶。

using (PrincipalContext ctx = new PrincipalContext(
  ContextType.Domain, "my.domain", "DC=my,DC=domain")) {

  if (ctx != null) {
    using (GroupPrincipal gp = GroupPrincipal.FindByIdentity(ctx, "UserGrp1")) {
      // Get all group members
      PrincipalSearchResult<Principal> psr = gp.GetMembers();
      foreach (Principal p in psr) {
         // other logic 
      }
    }
  }
}

预先感谢我在这方面收到的任何帮助。


通过使用来做到这一点UserPrincipal.GetGroups();

对于完整的代码,这里是

/// <summary>
/// Gets a list of the users group memberships
/// </summary>
/// <param name="sUserName">The user you want to get the group memberships</param>
/// <returns>Returns an arraylist of group memberships</returns>
public ArrayList GetUserGroups(string sUserName)
{
    ArrayList myItems = new ArrayList();
    UserPrincipal oUserPrincipal = GetUser(sUserName);

    PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
    }
    return myItems;
}



/// <summary>
/// Gets a certain user on Active Directory
/// </summary>
/// <param name="sUserName">The username to get</param>
/// <returns>Returns the UserPrincipal Object</returns>
public UserPrincipal GetUser(string sUserName)
{
    PrincipalContext oPrincipalContext = GetPrincipalContext();

    UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
    return oUserPrincipal;
}


/// <summary>
/// Gets the base principal context
/// </summary>
/// <returns>Retruns the PrincipalContext object</returns>
public PrincipalContext GetPrincipalContext()
{
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
    return oPrincipalContext;
}

或者要获取完整的 AD 参考,请访问here http://anyrest.wordpress.com/2010/06/28/active-directory-c/.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 GroupPrincipal 查找用户 的相关文章

随机推荐

  • 关于 java 中复杂结构的建议(DAO 和服务层链接/耦合)

    介绍 我正在尝试使用 Java 接口 抽象类和泛型创建一个相当复杂的结构 由于没有泛型经验 并且在创建良好的 OOP 设计方面只有一般经验 这开始被证明是一个相当大的挑战 我有一种感觉 我想做的事实际上无法完成 但我可以足够接近它 我将尝试
  • 上传并 POST 文件到 PHP 页面

    我需要一种上传文件并将其 POST 到 php 页面的方法 php 位于 apache 服务器上 我在我的桌面应用程序中使用这个 C 代码 这是一个c代码的套接字 当我执行程序时 可以得到 文件大小为772906 打开服务器正常 文件大小为
  • 将音频添加到 OpenCV 生成的视频中

    我一直在使用python下的OpenCV http opencv willowgarage com documentation python index html从捕获设备录制视频 每小时写入两个输出 AVI 每小时开始时文件名都会更改 其
  • 命令“php”无法识别,但它已在 Windows PATH 中注册

    我在 Windows 服务器上的 PATH 字符串中设置了 C Ampps php 但是当我在 PHPStorm 终端上键入 php 时 总是给出 php is not recognized as an internal or extern
  • excel vba - 在电子表格上查询

    如果我有这两张表 is there some sort of excel vba code using ADO that could acheive these desired results which could utilise any
  • 允许跨域ajax请求

    在我的项目中 我需要允许其他人向我的脚本发送 ajax 请求 因此 外部请求可能来自其他网站和域 也可能来自浏览器扩展 我在脚本顶部简单地添加了这两行 让他们执行此操作 header Access Control Allow Origin
  • Websocket 的基本身份验证

    当我使用 chrome 创建新的 websocket 时 new WebSocket ws gert email protected cdn cgi l email protection 8001 dbname Nodejs服务器接收 GE
  • 如何使用 makecert 创建证书颁发机构证书?

    我正在尝试创建一个使用 SSL 和自签名证书的网站 这就是我所做的 创建权威证书 makecert n CN root signing authority r sv root pvk root cer 创建目标证书 makecert r p
  • NSTimer 在后台的行为(addTimer:, beginBackgroundTaskWithExpirationHandler:)

    Xcode 6 3 1 ARC 启用 适用于 iOS 8 3 我需要帮助理解我在应用程序进入后台后尝试在应用程序中维护单例共享计时器时遇到的奇怪行为 以前我不关心这个 NSTimer 因为它是使用后台位置服务在后台更新用户位置的 但是 我想
  • 检测图像中的透明玻璃

    计算机视觉文献中是否有任何方法可以检测图像中的透明玻璃 就像如果我有汽车的图像 我可以检测窗户吗 ETC 到目前为止我发现的所有方法都是主动方法 即需要校准 对环境或激光的控制 我需要一种被动方法 即您拥有的只是一个图像 或多视图图像的对象
  • 数据框中的动物园滞后差异

    我想将滞后差异的结果放回到我的数据框中 这意味着我将拥有针对不同滞后的领先 NA 我在用 new df lag1 lt diff new df Close lag 1 differences 1 arithmetic TRUE na pad
  • html2canvas z-index 不起作用

    我正在使用 html2canvas 库将 div 转换为 png 我有一个关于z index 正如你在图片上看到的 我有盒子 在一个盒子里我有一个用于背景着色的 div z index 0 它随着高度值和数字的 div z index 1
  • 我可以跳过 HTML5 中“style”标签中的属性“type”吗? [复制]

    这个问题在这里已经有答案了 根据W3学校 http www w3schools com tags tag script asp 我可以跳过属性type对于标签script在 HTML5 中 Evidence HTML 4 01 和 HTML
  • 像 TreeView Android 一样的 ExpandableListview

    嘿我正在实施的人们TreeView using ExpandableListView 但不幸的是我有一些测量问题无法解决 这是问题的屏幕截图 可以看到测量存在问题 由于我是 Android 新手 我不太明白onMeasure 方法 我有 1
  • starsGeometry[0].setAttribute 不是函数

    尝试复制此站点上存在的 Three js 地球仪 https trijs org examples misc controls fly html https threejs org examples misc controls fly ht
  • IIS 中有 ASP.NET 网站管理工具吗?

    我正在使用 asp net 网站管理工具来管理项目中的不同角色 当前是客户和管理员 在vs 2008的开发过程中 管理角色非常容易 项目 gt Asp Net 配置 部署系统 IIS 时如何管理我的角色和用户 是否可以在 IIS 上运行 a
  • 使用文本模式密码的文本框不显示文本 asp.net c#

    我的网络表单上有几个按钮 当用户单击它们时 他们将更新文本框 这一直有效 直到我添加了 textmode 密码 现在文本框不再显示文本 我调试了应用程序 文本属性正在获取值 但它再次没有显示 这是我尝试过的 protected void b
  • 路由器无法与 Link 一起使用(React Router Dom 6)

    所以我在一个新的原始 CRA 5 typescript 应用程序上对此进行了测试 在里面App tsx我刚刚将其内容替换为 import Router Routes Route Link from react router dom impo
  • 如何对每个菜单项应用不同的包装?

    我试图在 TypoScript 中创建一个自定义菜单 我的想法是有一个div对于每个菜单项 Example lib mainMenu HMENU lib mainMenu entryLevel 0 1 TMENU 1 NO allWrap
  • 通过 GroupPrincipal 查找用户

    在我的 Active Directory my domain 中 我有许多组 UserGrp1 UserGrp2 等 其中有许多用户 一个用户可以存在于多个组中 我目前的代码允许我使用 GroupPrincipal 类来查找组 然后从那里获