Tips, tricks, and frustrations from my years of building ASP.NET websites.

Thursday, April 10, 2008

Using the ADSI class IADsGroup in .NET

Want to call the ADSI function IADsGroup.IsMember(x) in .NET. There is an easy way to do this but the solution is not well known. Check out my function IsMember(user,group):


public bool IsMember(string aUser, string aGroup)
{
  if ((String.IsNullOrEmpty(aUser) == true) || (String.IsNullOrEmpty(aGroup) == true))
  {
  return false;
  }


  aUser = aUser.Trim().Replace('\\', '/');
  aGroup = aGroup.Trim().Replace('\\', '/');


  System.DirectoryServices.DirectoryEntry de = new System.DirectoryServices.DirectoryEntry(String.Format("WinNT://{0},group", aGroup));


  object[] obj = new object[1];
  obj[0] = String.Format("WinNT://{0}", aUser);


  // Call the IADsGroup.IsMember() function.
  return (bool)de.Invoke("IsMember", obj);
}

No comments:

About Me

I'm currently employed as a Senior Systems Programmer/Analyst and sometimes Project Manager at Texas Instruments Inc. in Plano TX.