C# 获取用户在 Active Directory 中所属的组

标签 c# active-directory

我不是天生的程序员,所以我提前道歉 :) 我正在使用来自 http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C#39 的代码片段这真的很有帮助。我正在使用他的方法获取用户组成员资格,它也需要他的 AttributeValuesMultiString 方法。我没有任何语法错误,但是当我通过 Groups("username", true) 调用 Groups 方法时,出现以下错误:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in System.DirectoryServices.dll

我已经做了一些挖掘,但似乎没有什么能真正回答我为什么会收到这个错误。

最佳答案

您应该查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在这里阅读所有相关信息:

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

    if(user != null)
    {
       // get the user's groups
       var groups = user.GetAuthorizationGroups();

       foreach(GroupPrincipal group in groups)
       {
           // do whatever you need to do with those groups
       }
    }

}

新的 S.DS.AM 使得在 AD 中与用户和组一起玩真的很容易!

关于C# 获取用户在 Active Directory 中所属的组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25751176/

相关文章:

c# - 从 DN 获取用户信息

c# - 上下文在代码优先模式下使用,代码是从 EDMX 文件生成的,用于数据库优先或模型优先开发

c# - MvvmCross MvxListView 不刷新项目删除

c# - XXTEA的返回值

c# - 在 C# 中排除 Active Directory 的 PrincipalSearcher QueryFilter 中的空结果

c# - ASP.NET 5/MVC 6 本地事件目录

powershell - 根据列表禁用的AD用户

c# - 使用 Azure AD Graph 客户端获取 Active Directory 管理员

javascript - 时间选择器控件

c# - 如何连接 ReadOnlyCollection