.net - 从 Active Directory 获取用户的组成员资格

标签 .net windows active-directory

如何从 AD 获取用户的组成员身份,最好使用与获取用户的 Department 属性相同的模式,如下所示?我找到了几个例子,但是所有例子技术的交集很小,缺乏这个部门查询的紧密性和简单性:

        var adServer = ConfigurationManager.AppSettings["adServer"] ?? "localhost";
        var remoteRoot = new DirectoryEntry(GetRootPath(adServer));
        var searcher = new DirectorySearcher(remoteRoot, string.Format("(SAMAccountName={0})", shortUserName));

        searcher.PropertiesToLoad.Add("Department");
        SearchResult result = null;
        result = searcher.FindOne();

最佳答案

您使用的是 .NET 3.5 吗?如果是这样,那很容易:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

string userName = "yourUser";

UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);

PrincipalSearchResult<Principal> results = user.GetAuthorizationGroups();

找到您的用户,然后对您的用户主体调用 .GetAuthorizationGroups() - 返回用户所属的所有组,包括他的主要组和任何嵌套组成员身份。

看看这个 MSDN article了解 .NET 3.5 在处理 AD 方面的更多新优点。

在 .NET 2.0 中,事情变得更加困惑......

关于.net - 从 Active Directory 获取用户的组成员资格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1974023/

相关文章:

c# - 什么是多态?

.net - DialogResult 关闭设置对通过 ShowDialog() 显示的表单有影响吗?

c++ - 在许多 malloc/free 小块内存之后 malloc 大块内存失败

windows - 添加特定于应用程序的路径,因此它可以从 Windows 中的命令行运行

active-directory - 如何使用远程 LDAP 客户端确定 Active Directory 服务器的*版本*?

c# - 如何使用 Principal Context 连接到 Active Directory?

.net - JSON.net 不包括 EmitDefaultValue 为 false 的空属性

.net - 将异常和其他错误记录到.Net Core API中的txt文件的最佳方法

windows - 在haskell中删除文件

php - 从 php 文件执行 Powershell 脚本