private IsUserInGroup ( System.DirectoryServices.AccountManagement.UserPrincipal user, |
||
user | System.DirectoryServices.AccountManagement.UserPrincipal | |
groupInfo | ||
return | bool |
/// <summary> /// Returns a list of groups of which the user is a member. It does so in a fashion that /// may seem strange since one can call UserPrincipal.GetGroups, but seems to be much faster /// in my tests. /// </summary> /// <param name="user"></param> /// <returns></returns> private static List <GroupPrincipal> GetGroups(UserPrincipal user) { List <GroupPrincipal> result = new List <GroupPrincipal>(); // Get all groups using a PrincipalSearcher and GroupPrincipal filter = new GroupPrincipal(m_machinePrincipal); using (PrincipalSearcher searcher = new PrincipalSearcher(filter)) { PrincipalSearchResult <Principal> sResult = searcher.FindAll(); foreach (Principal p in sResult) { if (p is GroupPrincipal) { GroupPrincipal gp = (GroupPrincipal)p; if (LocalAccount.IsUserInGroup(user, gp)) { result.Add(gp); } else { gp.Dispose(); } } else { p.Dispose(); } } } return(result); }