1

Im writtem app that looks for a user in active directory based on there name

When I try to create new PrincipalContext with ContextType.Domain and the domain name as a string, I get the exception "The server could not be contacted."

So to get the the AD that I did this ... Open System by clicking the Start button Picture of the Start button, clicking Control Panel, clicking System and Maintenance, and then clicking System. Which I got from http://windows.microsoft.com/en-gb/windows-vista/find-the-domain-your-computer-is-on

Which gave me Something.Something (not this, but two strings with a . between them)

So when I run the following code and enter Something.Something as the domain, I get the exception "The server could not be contacted." on the new PrincipalContext(ContextType.Domain, domain); I have tried changing the case of the string and the but dont seem to have any luck.

So what should I be using for the domain?

    public static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Enter Domain, then press enter.");
            string domain = Console.ReadLine();

            Console.WriteLine("Enter First Name, then press enter.");
            string userName = Console.ReadLine();

            //This is the line that always crashes throws error
            var principalContext = new PrincipalContext(ContextType.Domain, domain);
            var user = UserPrincipal.FindByIdentity(principalContext, userName);

            if (user == null)
            {
                Console.WriteLine("User Not Found");
            }

            var groups = user.GetGroups(principalContext);
            var result = new List<string>();
            groups.ToList().ForEach(sr => result.Add(sr.SamAccountName));

            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
Ashley Kilgour
  • 1,110
  • 2
  • 15
  • 33
  • If your machine is already joined to the domain, you should be able to use `var principalContext = new PrincipalContext(ContextType.Domain);`. – Jack A. Apr 13 '16 at 20:11

2 Answers2

1

Ashley is correct... assuming you are running the application on a machine that is joined to the domain.

using System;
using System.DirectoryServices.AccountManagement;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter First Name, then press enter.");
            var userName = Console.ReadLine();

            // Will search the domain the application is running on
            var principalContext = new PrincipalContext(ContextType.Domain);
            var user = UserPrincipal.FindByIdentity(principalContext, userName);

            if (user == null)
            {
                Console.WriteLine("User Not Found");
            }
            else
            {
                // Gets a list of the user's groups
                var groups = user.GetGroups().ToList();

                // Loops the groups and prints the SamAccountName
                groups.ForEach(g => Console.WriteLine(g.SamAccountName));
            }

            Console.ReadKey();
        }
    }
}
Stephen McDowell
  • 839
  • 9
  • 21
  • Cheers, I tried that and get the exception "The server could not be contacted." on the new PrincipalContext(ContextType.Domain) line. That is what made me try adding the Domain – Ashley Kilgour Apr 13 '16 at 23:18
  • I tried my code on a no domain machine and got your same error. Try running this to see what you get. Should be something like {domain}\\{userName}. var name = System.Security.Principal.WindowsIdentity.GetCurrent().Name; – Stephen McDowell Apr 14 '16 at 01:39
0

If you've got several seconds to spare waiting for your data form a large AD, then go ahead and use PrincipalContext but if you want your response in milliseconds, use DirectoryEntry, DirectorySearcher and .PropertiesToLoad.

Here's an example for getting the groups for a user:

https://stackoverflow.com/a/65986796/5248400

MikeZ
  • 1,155
  • 1
  • 13
  • 20