Check if user exists in Active Directory

I need to check if the user exists in AD, and if so, get some information about the user. I was able to do this as shown below. But it is very slow. Is there any way to do this faster?

Thank!

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter AD account name...");
            string strADLoginName = Console.ReadLine();

            using(PrincipalContext context = new PrincipalContext(ContextType.Domain,"DEVMC"))
            {
                using (UserPrincipal user = UserPrincipal.FindByIdentity(context, strADLoginName))
                {
                    bool userExists = (user != null);
                    if (userExists)
                    {
                        Console.WriteLine("User exists");
                        Console.WriteLine(user.EmailAddress);
                    }
                    else
                    {
                        Console.WriteLine("User doesn't exist");
                    }
                }


            }
            Console.ReadKey();
         }
     }
}
+3
source share
1 answer

Well, the only real approach you could take to make it faster would be for the PrincipalContext to be once created and cached for future use, so you don't need to re-create this context again and again, each time you call this function.

- , . ? - ASP.NET Winforms, WPF, Silverlight?

0

Source: https://habr.com/ru/post/1749115/


All Articles