I am using below C# code to check whether the user is part of required domain member group.
The passing username is part of 3 member groups but the code is returning the first domain group member name and exit from the for loop. Please help me to get entire list of domain group for the user.
bool bReturn = false;
string sDomainName = System.Environment.UserDomainName;
using (PrincipalContext oContext = new PrincipalContext(ContextType.Domain, sDomainName))
{
if (oContext.ValidateCredentials(sUserName, sPassword))
{
using (PrincipalSearcher oSearcher = new PrincipalSearcher(new UserPrincipal(oContext)))
{
oSearcher.QueryFilter.SamAccountName = sUserName;
Principal oPrincipal = oSearcher.FindOne();
foreach (Principal oPrin in oPrincipal.GetGroups())
{
if (oPrin.Name.Trim().ToString().Equals(sGroupName))
{
bReturn = true;
break;
}
}
}
}