windows 找出用户属于哪些组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3672602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:13:20  来源:igfitidea点击:

find out user belongs to which groups

c#asp.netwindowslogin

提问by user175084

I have a windows user accounts which I just created take XYZ for example.

我有一个刚刚创建的 Windows 用户帐户,以 XYZ 为例。

This XYZ belongs to a User group and a custom group I created in Computer Management --> Local users and groups.

此 XYZ 属于我在计算机管理 --> 本地用户和组中创建的用户组和自定义组。

So in properties I see that the user belongs to the 2 groups.

所以在属性中我看到用户属于 2 个组。

Now I want to get those groups and display them. Any suggestions?

现在我想获取这些组并显示它们。有什么建议?

I have done this but this is not right as it gives me the roles of SQL (I think)

我已经这样做了,但这是不对的,因为它给了我 SQL 的角色(我认为)

here is what I did:

这是我所做的:

after logging in and impersonating I call the function

登录并模拟后,我调用该函数

getUserGroups();

private void getUserGroups()
{
    // collect the user domain and identity
    string[] arr =
        System.Web.HttpContext.Current.Request.
        LogonUserIdentity.Name.Split('\');

    // update the display to show
    // the captured domain and user
    if (arr.Length > 0)
    {
        new GUIUtility().LogMessageToFile("User Name" + arr[0].ToString());
        new GUIUtility().LogMessageToFile("User Domain" + arr[1].ToString());
    }

    // create an arraylist and populate
    // it with the list of groups that
    // the current user belongs to
    ArrayList al = new ArrayList();
    al = GetGroups();

    // check to see if the user belongs
    // to a specific group and create
    // a list of all of the user's groups
    foreach (string s in al)
    {
        // add this one to the list
        new GUIUtility().LogMessageToFile("Group" + s);
        // check to see if the user
        // belongs to a specific group

        //if (s == "BXSWLT\SomeCustomGroup")
        //{
        //    // change the label to show
        //    // there was a match
        //    lblMemberOfGroup.Text = "YES";
        //}
    }
}


public ArrayList GetGroups()
{
    ArrayList groups = new ArrayList();
    foreach (System.Security.Principal.IdentityReference group in
    System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
    {
        groups.Add(group.Translate(typeof
        (System.Security.Principal.NTAccount)).ToString());
    }
    return groups;
}

the Result I get is:

我得到的结果是:

9/8/2010 5:57:22 PM: User Name  NT AUTHORITY.
9/8/2010 5:57:22 PM: User Domain  IUSR.
9/8/2010 5:57:22 PM: Group  Everyone.
9/8/2010 5:57:22 PM: Group  BUILTIN\Users.
9/8/2010 5:57:22 PM: Group  NT AUTHORITY\Authenticated Users.
9/8/2010 5:57:22 PM: Group  NT AUTHORITY\This Organization.
9/8/2010 5:57:22 PM: Group  LOCAL.

回答by Johann Blais

Did you try with

你试过吗

HttpContext.Current.User.Identity

instead of

代替

HttpContext.Current.Request.LogonUserIdentity

?

?