如何在Active Directory中获取用户组?(C#,asp.net)

我使用此代码来获取当前用户的组。但是我想手动给用户,然后得到他的组。我怎样才能做到这一点?


using System.Security.Principal;


public ArrayList Groups()

{

    ArrayList groups = new ArrayList();


    foreach (IdentityReference group in System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)

    {

        groups.Add(group.Translate(typeof(NTAccount)).ToString());

    }


    return groups;

}


aluckdog
浏览 719回答 3
3回答

慕仙森

GetAuthorizationGroups()找不到嵌套的组。要真正获得给定用户所属的所有组(包括嵌套组),请尝试以下操作:using System.Security.Principalprivate List<string> GetGroups(string userName){&nbsp; &nbsp; List<string> result = new List<string>();&nbsp; &nbsp; WindowsIdentity wi = new WindowsIdentity(userName);&nbsp; &nbsp; foreach (IdentityReference group in wi.Groups)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.Add(group.Translate(typeof(NTAccount)).ToString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex) { }&nbsp; &nbsp; }&nbsp; &nbsp; result.Sort();&nbsp; &nbsp; return result;}我try/catch之所以使用,是因为在一个非常大的广告中,我在200个组中有2个例外,因为有些SID不再可用。(该Translate()调用执行SID->名称转换。)
打开App,查看更多内容
随时随地看视频慕课网APP