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:-

using

System.DirectoryServices; 

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

                  try

                  {

                        domain=alias.Split(delimiter,2);

                        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())

                        {

 

                              if(sResultSet.Properties.Contains(“cn”))

                              {

                                    //assign fullname

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

                                                     

                              }

 

                       

                              if(sResultSet.Properties.Contains(“givenName”))

                              {

                                    //assign firstname

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

                                                     

                              }

                 

                              if(sResultSet.Properties.Contains(“sn”))

                              {

                                    //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();

aliaswindowIdentity.Name;

try

{

DirectorySvcHelper userAuthetnicate  =  new DirectorySvcHelper();

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

}

catch( Exception ex )

{

throw ex;

}

 

Advertisements

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