Friday, January 5, 2018

Check if a user is in an AD group with C#.

This functions checks weather a user account is in an AD group or not.  It will return true if the user account is in that group and false if not.

public static bool verifygroup(string s)
{
AppDomain myDomain = Thread.GetDomain();
myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal group = (WindowsPrincipal)Thread.CurrentPrincipal;
     if (group.IsInRole(s) == true)
     {
          return true;
     }
     else
     {
     return false;
     }
}

if(verifygroup("mydomain\\<ad group>") == true)
{
     do something here..
}