.NET Framework Bookmark and Share   
 index > .NET Base Class Library > help with active directory
 

help with active directory

hi all.

if my user enters he's password in a textbox, how can i check it in the active directory (with c#)

shimshon

Hi hope my following code would help you. you can use this function directly once you include system.directoryservices

/// <summary>

/// Following method authenticate username and password against entry in active directory (LDAP)

/// returns true if valid user else false.

/// this function need using System.DirectoryServices;

/// </summary>

/// <param name="userName">Network login id</param>

/// <param name="password">Login password</param>

/// <returns></returns>

private bool IsValidUser(string userName, string password)

{

DirectoryEntry de_root = new DirectoryEntry("LDAP://rootDSE");

DirectoryEntry my_LDAP = new DirectoryEntry("LDAP://" + de_root.Properties["defaultNamingContext"].Value.ToString(), userName, password);

DirectorySearcher ds_searcher = new DirectorySearcher(my_LDAP);

try

{

ds_searcher.SearchScope = SearchScope.Subtree;

ds_searcher.Filter = "(&(objectClass=User) (SAMAccountName=" + userName + "))";

SearchResult sr_result = ds_searcher.FindOne();

return true;

}

catch (DirectoryServicesCOMException ex)

{

messagebox.show("Login Exception");

return false;

}

catch(Exception ex)

{

messagebox.show("Login Exception");

return false;

}

finally

{

//Do not delete following three lines:

de_root.Dispose();

my_LDAP.Dispose();

ds_searcher.Dispose();

}

}

sharadg

Hi hope my following code would help you. you can use this function directly once you include system.directoryservices

/// <summary>

/// Following method authenticate username and password against entry in active directory (LDAP)

/// returns true if valid user else false.

/// this function need using System.DirectoryServices;

/// </summary>

/// <param name="userName">Network login id</param>

/// <param name="password">Login password</param>

/// <returns></returns>

private bool IsValidUser(string userName, string password)

{

DirectoryEntry de_root = new DirectoryEntry("LDAP://rootDSE");

DirectoryEntry my_LDAP = new DirectoryEntry("LDAP://" + de_root.Properties["defaultNamingContext"].Value.ToString(), userName, password);

DirectorySearcher ds_searcher = new DirectorySearcher(my_LDAP);

try

{

ds_searcher.SearchScope = SearchScope.Subtree;

ds_searcher.Filter = "(&(objectClass=User) (SAMAccountName=" + userName + "))";

SearchResult sr_result = ds_searcher.FindOne();

return true;

}

catch (DirectoryServicesCOMException ex)

{

messagebox.show("Login Exception");

return false;

}

catch(Exception ex)

{

messagebox.show("Login Exception");

return false;

}

finally

{

//Do not delete following three lines:

de_root.Dispose();

my_LDAP.Dispose();

ds_searcher.Dispose();

}

}

sharadg

You can use google to search for other answers

Custom Search

More Threads

• Directory.GetFiles method
• Communication Between forms
• Adding a property to the datatable class
• Invoking operators by reflection?
• .NET User control Access Permission
• splitting web.config file
• How to start the scheduled task in the remote computer?
• fast user switching
• How to maintain responsive UI while doing thread-safe calls to controls?
• Public variable vs Property variables