I am trying to write a web service in C# that takes a username and AD group name, and returns true or false as a result. Right now I'm doing something to the effect of this.
public static Boolean CheckGroupForUser(String username, String groupname) {
Boolean Success = false;
try
{
using (PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain))
using (UserPrincipal user = UserPrincipal.FindByIdentity(oPrincipalContext, username))
using (GroupPrincipal gp = GroupPrincipal.FindByIdentity(oPrincipalContext, groupname))
{
Success = user.IsMemberOf(gp);
}
}
catch (Exception)
{
Success = false;
}
return Success;
}
This works fine. Returns true if the user is in the group, returns false if they are not. The problem is this.
Lets say I have an AD Group called TestGroup. TestGroup has 1 user named Bob in it.
CheckGroupForUser("Bob","TestGroup"); //true
Lets say user Bob is also in the AD Group OtherGroup. So I take Bob out of TestGroup, but add OtherGroup to TestGroup. So now technically Bob is in TestGroup, but not directly.
CheckGroupForUser("Bob","TestGroup"); //false
This is where I need help. I need this scenario to be true. Catch my drift? Any suggestions?