Posted on: 6/29/2015 8:31:40 PM | Views : 678

I am trying to get all groups in AD that a person is a member of, The following code gives me an error at the foreach loop.  Do not know how to resolve at this point, any help is appreciated.  Thank you

public static DataTable GetGroupmembership(string samAccount) { DataTable theDt = new DataTable(); theDt.Columns.Add("groupName"); using (PrincipalContext dmnctx = new PrincipalContext(ContextType.Domain)) { var user = UserPrincipal.FindByIdentity(dmnctx, IdentityType.SamAccountName, samAccount); if (user != null) { foreach (GroupPrincipal p in user.GetGroups()) { theDt.Rows.Add(new string[] { p.SamAccountName }); } } ...

Go to the complete details ...