find out user belongs to which groups

2020-08-01 11:08发布

问题:

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

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

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

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)

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.

回答1:

Did you try with

HttpContext.Current.User.Identity

instead of

HttpContext.Current.Request.LogonUserIdentity

?