views:

208

answers:

2

Hello,

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

Thanks!

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();
         }
     }
}
A: 

Well, the only real approach you could tak to make this faster would be to have the "PrincipalContext" be constructed once somewhere and cached for future use, so you don't have to re-create that context over and over again, every time you call that function.

Other than that - no, I don't see much room for improvement right here and now. What kind of app is this?? ASP.NET web apps, or Winforms, WPF, Silverlight??

marc_s
A: 

This is for ASP.NET web app.

K.R.