private IsUserInGroup ( string username, string groupname ) : bool | ||
username | string | |
groupname | string | |
return | bool |
private bool IsUserInGroup(string username, string groupname)
{
using(GroupPrincipal group = GetGroupPrincipal(groupname))
{
if (group == null) return false;
using(UserPrincipal user = GetUserPrincipal(username))
{
if (user == null) return false;
return IsUserInGroup(user, group);
}
}
}
LocalAccount::IsUserInGroup ( System.DirectoryServices.AccountManagement.UserPrincipal user, |
|
LocalAccount::IsUserInGroup ( System.DirectoryServices.AccountManagement.UserPrincipal user, System.DirectoryServices.AccountManagement.GroupPrincipal group ) : 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); }