i have this code to check for group membership but it seems to be taking too long to respond and slowing down my app, it takes almost 7-12 sec to respond, i just need to check for one particular group membership, is there a faster way to do this?
public static bool isInRole(UserAccount userAccount, string groupName)
{
using (var ctx = new PrincipalContext(ContextType.Domain, userAccount.DomainName))
{
using (var grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName))
{
bool isInRole = grp != null &&
grp
.GetMembers(true)
.Any(m => m.SamAccountName == userAccount.UserName);
return isInRole;
}
}
I don’t have your specific AD at hand to test this – but it might be worth an attempt: instead of checking the group’s members for a particular users (there could potentially be thousands of members), why don’t you check the user’s group membership to see if the user has the right group??
Something like :
Maybe that way around things will be a bit faster?