【问题标题】:find out user belongs to which groups找出用户属于哪些组
【发布时间】:2011-04-10 00:17:58
【问题描述】:

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

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

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

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

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

这就是我所做的:

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

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;
}

我得到的结果是:

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.

【问题讨论】:

标签: c# asp.net windows login


【解决方案1】:

你试过了吗

HttpContext.Current.User.Identity

而不是

HttpContext.Current.Request.LogonUserIdentity

?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2017-02-13
    相关资源
    最近更新 更多