-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: Jayant_Magic
Message 2 in Discussion

Hi Sangeetha,   To develop LDAP authentication code to look up the user in 
Active Directory  Add a new C# class file called LdapAuthentication.cs. 
Add a reference to the System.DirectoryServices.dll assembly. 
Add the following using statements to the top of LdapAuthentication.cs. 
 Copy Code
using System.Text;
using System.Collections;
using System.DirectoryServices; Rename the existing namespace as FormsAuthAD. 
Add two private strings to the LdapAuthentication class; one to hold the LDAP 
path to Active Directory and the other to hold a filter attribute used for 
searching Active Directory. 
 Copy Code
private string _path;
private string _filterAttribute; Add a public constructor that can be used to 
initialize the Active Directory path. 
 Copy Code
public LdapAuthentication(string path)
{
  _path = path;
} Add the following IsAuthenticated method that accepts a domain name, user 
name and password as parameters and returns bool to indicate whether or not the 
user with a matching password exists within Active Directory. The method 
initially attempts to bind to Active Directory using the supplied credentials. 
If this is successful, the method uses the DirectorySearcher managed class to 
search for the specified user object. If located, the _path member is updated 
to point to the user object and the _filterAttribute member is updated with the 
common name attribute of the user object. 
 Copy Code
public bool IsAuthenticated(string domain, string username, string
  pwd)
{
  string domainAndUsername = domain + @"\" + username;
  DirectoryEntry entry = new DirectoryEntry( _path, 
                                             domainAndUsername,
                                               pwd);   try
  { 
    // Bind to the native AdsObject to force authentication.
    Object obj = entry.NativeObject;
    DirectorySearcher search = new DirectorySearcher(entry);
    search.Filter = "(SAMAccountName=" + username + ")";
    search.PropertiesToLoad.Add("cn");
    SearchResult result = search.FindOne();
    if(null == result)
    {
      return false;
    }
    // Update the new path to the user in the directory
    _path = result.Path;
    _filterAttribute = (String)result.Properties["cn"][0];
  }
  catch (Exception ex)
  {
    throw new Exception("Error authenticating user. " + ex.Message);
  }
  return true;
} 
Also you can visit following link:   
http://msdn2.microsoft.com/en-us/library/aa302397.aspx#secnetht02_step3 
http://www.codeproject.com/dotnet/activedirusers.asp?df=100&forumid=127806&exp=0&select=1225851
   Regards
Jayant Jindal


-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member 
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to