LDAP verification

Posted: June 7, 2006 in Uncategorized

Often this is a common requirement to validated the user from the active directory or LDAP.Here by i am showing you a simple routine which will take the user alias for the domain and retrieve the userinfo:-

Fisrt of all you need to import the directory class:-



Keep this Routine in a Class let say DirectoryServiceHelper:-


public static void GetUserInfo(string alias,out string fullname,out string firstname,out string lastname)



                  string delimStr            =  @”\”;

                  char [] delimiter          =  delimStr.ToCharArray();

                  string[] domain;


                  //To get the domain name




                        string LookUpDirectory     =”LDAP://” +domain[0];


                        DirectoryEntry entry       = new DirectoryEntry(LookUpDirectory);

                        DirectorySearcher dSearch  = new DirectorySearcher(entry);


                        //Filter by alias name

                        dSearch.Filter             = “(mailnickname= ” + domain[1] + “)”;

                        fullname                   = “”;

                        firstname                  = “”;

                        lastname                   = “”;


                        foreach(SearchResult sResultSet in dSearch.FindAll())





                                    //assign fullname

                                    fullname          = sResultSet.Properties[“cn”][0].ToString() ;







                                    //assign firstname

                                    firstname          = sResultSet.Properties[“givenName”][0].ToString() ;






                                    //Assign lastname

                                    lastname          = sResultSet.Properties[“sn”][0].ToString() ;







                  catch ( Exception ex )


                        throw ex;




and in ur Main Routne or Startup Routine validet the user:-


public void AuthenticateUser()



string  alias;

string  firstName;

string  lastName;

string  name;

WindowsIdentity windowIdentity =  WindowsIdentity.GetCurrent();




DirectorySvcHelper userAuthetnicate  =  new DirectorySvcHelper();

DirectorySvcHelper.GetUserInfo( alias, out name, out  firstName, out  lastName );


catch( Exception ex )


throw ex;




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s