C# Active Directory Group Querying
- by user1073912
I am trying the code found here.
I am getting the following compile time error:
The name 'p' does not exist in the current context
Here is my code...can someone help? Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
public static List<string> GetGroups()
{
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
using (p = Principal.FindByIdentity(ctx, "yourUserName"))
{
var groups = p.GetGroups();
using (groups)
{
foreach (Principal group in groups)
{
Console.WriteLine(group.SamAccountName + "-" + group.DisplayName);
}
}
}
}
}