Google


Earn money without investing 1$

lovebird-mails.com lovebird-mails.com

Wednesday, August 8, 2007

Check Valid user using LDAP

The below function will authenticate user using Active Directory.


public bool IsAuthenticated(string domain, string username, string pwd)

{

string _path;

string _filterAttribute;

string servername = "ServerName"; // Give the server Name

string domainAndUsername = domain + "\\" + username;

DirectoryEntry entry = new DirectoryEntry("LDAP://" + servername, domainAndUsername, pwd);

try

{

object obj = entry.NativeObject;

DirectorySearcher search = new DirectorySearcher(entry);

search.Filter = "(SAMAccountName=" + username + ")";

search.PropertiesToLoad.Add("cn");

SearchResult result = search.FindOne();

if (result == null)

{

return false;

}

_path = result.Path;

_filterAttribute = ((string)(result.Properties["cn"][0]));

}

catch (Exception ex)

{

return false;

}

return true;

}

1 comment:

Romias said...

Hi, I was looking for something like your code, but for some reason yours didn't worked to me.

Which version of .NET Fwk are you using? 1.1, 2.0 ?

Thanks!

Review Books

  • ASP.NET WebService
  • Learn VisualStudio.net
  • Basic .NET Framework
  • Basic C#
  • .NET Interview Questions
  • C# 2005 Programming