找出用户所属的组 [英] find out user belongs to which groups

查看:169
本文介绍了找出用户所属的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我刚刚创建采取XYZ例如Windows用户帐户



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



因此,在性能我看到,用户所属的2组。



现在我想要得到的组,并显示它们。有什么建议?



我已经做到了这一点,但是这是不对的,因为它给我SQL(我认为)


$的角色b $ b

下面是我做过什么:



登录并冒充我调用函数



<后pre> getUserGroups();

私人无效getUserGroups()
{
//收集用户域和身份
的String [] =改编
System.Web.HttpContext.Current 。请求。
LogonUserIdentity.Name.Split('\\');

//更新显示,以显示
//捕获的域和用户
如果(arr.Length大于0)
{
新GUIUtility ().LogMessageToFile(用户名+常用3 [0]的ToString());
新GUIUtility()LogMessageToFile(用户域+常用3 [1]的ToString());
}

//创建一个ArrayList并填充
//它与
组的列表//当前用户所属了
的ArrayList人=新的ArrayList();
人= GetGroups();

//检查用户是否属于
//给特定组,并创建
//(所有的用户群
的foreach列表字符串S IN人)
{
//添加这一个列表
新GUIUtility()LogMessageToFile(「本集团」+ S)。
//检查是否用户
//属于特定组

//如果(S ==BXSWLT\\SomeCustomGroup)
// {
// //更改标签,以显示
// //有一个匹配
// lblMemberOfGroup.Text =YES;
//}
}
}


公众的ArrayList GetGroups()
{
组的ArrayList =新的ArrayList() ;
的foreach(System.Security.Principal.IdentityReference组
System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
{
groups.Add(group.Translate( typeof运算
(System.Security.Principal.NTAccount))的ToString());
}
组的回报;
}



结果我得到的是:



  9/8/2010下午5时57分22秒:用户名NT AUTHORITY。 
9/8/2010下午5时57分22秒:用户域IUSR。
9/8/2010下午5时57分22秒:Everyone组。
9/8/2010下午5时57分22秒:集团BUILTIN\Users。
9/8/2010下午5时57分22秒:集团NT AUTHORITY\Authenticated用户。
9/8/2010下午5时57分22秒:集团NT AUTHORITY\This组织。
9/8/2010下午5时57分22秒:一群当地。


解决方案

你有没有试着用



  HttpContext.Current.User.Identity 

而不是

  HttpContext.Current.Request.LogonUserIdentity 


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.

解决方案

Did you try with

HttpContext.Current.User.Identity

instead of

HttpContext.Current.Request.LogonUserIdentity

?

这篇关于找出用户所属的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆