【问题标题】:How do I find out if an arbitrary DOMAIN\username is in a specific role with C#?如何确定任意 DOMAIN\username 是否具有 C# 的特定角色?
【发布时间】:2011-09-12 20:36:26
【问题描述】:

我真的是 C# 新手,所以请原谅我的无知。我需要测试用户(域\用户名)是否在特定组中,是的,这包括嵌套组。

我发现WindowsPrincipal.IsInRole() 在处理当前登录用户时效果非常好。但对我来说不是这样。我需要能够传入任意 DOMAIN\username 或 UPN(我会做最容易实现的那个),如果他们是 X 组的成员,则返回 true/false,即使他们只是 X 组的间接成员组 X(例如:用户是组 Y 的成员,组 Y 是组 X 的成员)。

我看过WindowsIdentity,也许它对C# 来说是新的,但我只是没有看到像WindowsIdentity("MYDOMAIN\User1") 这样的方法。嗯,我做到了,但从来没有接近让它发挥作用。

使用 C#,给定一个 DOMAIN\username,它不是当前登录的用户,我如何确定他们是否是 DOMAIN\group 的成员?

【问题讨论】:

    标签: c# active-directory isinrole


    【解决方案1】:

    您可以为此使用 LDAP 查询。这是一篇好文章

    Howto: (Almost) Everything In Active Directory via C#

    【讨论】:

    • 感谢您提供该链接。虽然它没有为我提供我希望的解决方案,但它确实包含大量其他有用的信息。
    【解决方案2】:

    这是我使用过的一个功能,您应该可以按原样使用它。您可能必须创建 ParseUserDomain 但这很简单:

    /// <summary>
    /// Checks if a user in is a active directory group.
    /// <summary>
    /// <param name="username">Can contain the domain and username or just username
    ///    (eg. domain\username or username).  If no domain is specified, the default
    ///    domain is used.</param>
    /// <param name="group">Active directory group to check.  Group name only.  No
    ///    leading domain as the domain from the user is used.</param>
    /// <returns></returns>
    public bool UserIsInActiveDirectoryGroup(string username, string group)
    {
        bool isInGroup = false;
        string user = "";
        string domain = "";
        // Parses off domain and user to seperate values
        ParseUserDomain(username, out domain, out user);   
    
        if (string.IsNullOrEmpty(user) ||
            string.IsNullOrEmpty(domain) ||
            string.IsNullOrEmpty(group))
        {
            return false;
        }
    
        using (PrincipalContext ADContext = new PrincipalContext(ContextType.Domain,
            domain))
        {
            using (GroupPrincipal principalGroup = 
                GroupPrincipal.FindByIdentity(ADContext, group))
            {
                if (principalGroup != null)
                {
                    using (UserPrincipal ADPrincipalUser = 
                        UserPrincipal.FindByIdentity(ADContext, user))
                    {
                        // True means deep search
                        var users = principalGroup.GetMembers(true);
                        isInGroup = users.Contains(ADPrincipalUser);
                    }
                }
            }
        }
        return isInGroup;
    }
    

    【讨论】:

    • Kelsey:我尝试修改你的脚本,但它不会编译,当它遇到 users.Contains() 时会报错。说Error 5 'System.DirectoryServices.AccountManagement.PrincipalSearchResult&lt;System.DirectoryServices.AccountManagement.Principal&gt;' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'System.DirectoryServices.AccountManagement.PrincipalSearchResult&lt;System.DirectoryServices.AccountManagement.Principal&gt;' could be found (are you missing a using directive or an assembly reference?)
    • @Alan M 您使用的是什么版本的 C#?您是否定义了以下using 语句:using System.Linq;
    • 我尝试了 .NET Framework 2.0、3.5 和 4.0。这是在 Visual Studio 2010 中。我没有包含 System.Linq。我会把它绑在那里并试一试。
    • @Alan M 在我发布的示例中我使用的是 .NET 4.0 和 Linq。
    【解决方案3】:

    我在 Stack Overflow 中名为 Find Recursive Group Membership (Active Directory) using C# 的类似条目中回答了 recursive query。更改我在那里提供的代码可以让你做你想做的事。

    【讨论】:

      【解决方案4】:

      回答自己的问题:我尝试了提出的解决方案,但并不是让它们起作用。请注意,我 100% 确定这是由于我对 C# 缺乏经验,与评论者发布的内容无关。爱并感谢所有提供帮助的评论者。

      对我有用的是:http://ddkonline.blogspot.com/2010/05/how-to-recursively-get-group-membership.html

      我确实需要做一些基本的调整以使上述解决方案适合我的情况(例如更改 LDAP 参数),但它基本上是有效的。如果是组成员,则返回 true,否则返回 false。我希望这可以为未来的搜索者节省一些头发,因为我已经失去了一把。再次感谢所有发布帮助的人。

      【讨论】:

      • 抱歉,但您的文章中描述的“目录搜索过滤器中 Microsoft LDAP 提供的特殊查询语法以递归方式获取用户直接和间接所属的所有组的列表”是相同的作为我在Find Recursive Group Membership (Active Directory) using C#. 中给出的示例中使用的那个
      • 我会将其归咎于我对 C# 的缺乏经验。无论出于何种原因,我都能让它工作。
      猜你喜欢
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 2023-03-09
      • 2021-10-25
      • 2010-12-22
      • 2020-07-08
      • 2019-06-16
      相关资源
      最近更新 更多