1

我的第一个项目是为我们的 Active Directory 创建一个 Web 工具。我需要做的一件事是获取并显示显示在 Active Directory 中用户帐户的“MemberOf”选项卡中的组,并将其传递给 gridview。我做了一些家庭作业,这就是我所拥有的。

我有一个文本框(txtusername),旁边是一个按钮(“搜索此用户的组”)。

这是我的代码:

protected void btnsearch_Click(object sender, EventArgs e)
{
    ADMAM.GetUserGroups(txtusername.Text, Session["WindowsID"].ToString(), Session["Password"].ToString());
}

public DataTable GetUserGroups(string sUserName, string windowsid, string password)
{
    DataTable dt = new DataTable();
    UserPrincipal oUserPrincipal = GetUser(sUserName, windowsid, password);

    PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        dt.Rows.Add(oResult.Name);
        dt.AcceptChanges();
    }

    return dt;
}

我仍然无法在“MemberOf”选项卡中获取组。

4

1 回答 1

0

尝试使用.GetAuthorizationGroups调用:

PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetAuthorizationGroups();

foreach (Principal oResult in oPrincipalSearchResult)
{
    dt.Rows.Add(oResult.Name);
    dt.AcceptChanges();
}

This has the added benefit that it will recursively search through group memberships for a user (if the user is member of Group A which in turn is member of Group B, then .GetAuthorizationGroups() will also return Group B as a group the user is a member of - indirectly).

于 2013-03-15T10:04:27.000 回答