C# Active Directory Group Querying

Posted by user1073912 on Stack Overflow See other posts from Stack Overflow or by user1073912
Published on 2011-11-30T17:44:22Z Indexed on 2011/11/30 17:50 UTC
Read the original article Hit count: 412

Filed under:
|

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); 
                } 
            }
        }
        } 

    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about active-directory