如何确定任意 DOMAIN\用户名是否属于 C# 的特定角色?

发布于 2024-12-04 00:23:05 字数 481 浏览 1 评论 0原文

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

我发现在处理当前登录用户时,WindowsPrincipal.IsInRole() 效果非常好。但我的情况并非如此。我需要能够传入任意 DOMAIN\username 或 UPN(我将采用最容易实现的方式),并且如果它们是 X 组的成员,则返回 true/false,即使它们只是 X 组的间接成员组 X(例如:用户是组 Y 的成员,组 Y 是组 X 的成员)。

我研究过 WindowsIdentity,也许它对 C# 来说是新的,但我只是没有找到一种方法来执行 WindowsIdentity("MYDOMAIN\User1") 之类的操作。嗯,我做到了,但从未让它发挥作用。

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

I'm really new to C# so forgive my ignorance. I need to test if a user (DOMAIN\username) is in a particular group, and yes, this includes nested groups.

I have found that WindowsPrincipal.IsInRole() works fantastic, when dealing with the current logged-in user. That isn't the case for me though. I need to be able to pass in an arbitrary DOMAIN\username or UPN (I'll do whichever is easiest to implement), and get back true/false if they are a member of group X, even if they are only indirect members of group X (e.g: user is member of group Y, and group Y is member of group X).

I've looked at WindowsIdentity, and maybe it's being new to C#, but I just didn't see a way to do something like WindowsIdentity("MYDOMAIN\User1"). Well, I did, but never got anywhere close to getting it to work.

Using C#, given a DOMAIN\username, which will not be the current logged-in user, how can I determine if they are a member of DOMAIN\group ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星軌x 2024-12-11 00:23:06

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

Howto:(几乎)通过 C# 实现 Active Directory 中的所有内容

You can use LDAP query for that. Here is a good article

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

无尽的现实 2024-12-11 00:23:06

这是我使用过的一个有效的函数,您应该几乎可以按原样使用它。您可能必须创建 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;
}

Here is an function I have use that works and you should be able to use it as is almost. You will probably have to create ParseUserDomain but that is pretty straight forward:

/// <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;
}
☆獨立☆ 2024-12-11 00:23:06

我在一个递归查询中回答了Stack Overflow 中名为 Find Recursive 的类似条目使用 C# 的组成员身份 (Active Directory)。更改我在那里提供的代码可以让您做您想做的事情。

I answered with a recursive query in a similary entry in Stack Overflow called Find Recursive Group Membership (Active Directory) using C#. Changing the code I gave there can allow you to do what you want.

君勿笑 2024-12-11 00:23:06

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

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

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

Answer to own question: I tried the solutions presented, and wasn't to get them to work. Note, I'm 100% sure this is due to my inexperience with C#, and not anything to do with what the commenters posted. Love and thanks to all the commenters who helped out.

What did work for me is this: http://ddkonline.blogspot.com/2010/05/how-to-recursively-get-group-membership.html

I did have to do some basic tweaks to make the above solution fit my situation (change the LDAP params, for example), but it basically worked. Returns true if member-of-group, false otherwise. I hope this saves future searchers some hair, as I've already lost a handfull. Thanks again to all who posted help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文