Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to use LDAP in asp.net c# ?

<appsettings>
<add key="DirectoryPath" value="LDAP://XXXXXXXX,XXXXXX,XXXXXX">
<add key="DirectoryDomain" value="YY">



I got this code from google, please can you help me, what is

DirectoryPath
DirectoryDomain

value="LDAP://XXXXXXXX,XXXXXX,XXXXXX"
value="YY"

Thanks.
Posted
Updated 4-Sep-19 1:05am

First you need to System.DirectoryServices namespace, to check if the user is Authenticated in LDAP (active directory) then you can use below snippet
C#
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;
  }
 
Share this answer
 
Hi,

Kindly you can see the reference

Active Directory Authentication from ASP .NET[^]
 
Share this answer
 
 
Share this answer
 
Comments
Member239258 4-Jan-16 5:15am    
Sir, Same code am using.
But what i need to add in values.
Please help sir.

<appsettings>
<add key="DirectoryPath" value="LDAP://XXXXXXXX,XXXXXX,XXXXXX">
<add key="DirectoryDomain" value="YY">
Member239258 4-Jan-16 5:15am    
<appsettings>
<add key="DirectoryPath" value="LDAP://XXXXXXXX,XXXXXX,XXXXXX">
<add key="DirectoryDomain" value="YY">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900