Can we Pass LDAP string to instead of server-name, username & password

john john 1,021 Reputation points
2022-10-12T22:16:58.8+00:00

we have the following code to connect to Active Directory using PrincipleContext, where we pass the servername,username & password :-

string ADServerName = System.Web.Configuration.WebConfigurationManager.AppSettings["ADServerName"];  
string ADusername = System.Web.Configuration.WebConfigurationManager.AppSettings["ADUserName"];  
string ADpassword = System.Web.Configuration.WebConfigurationManager.AppSettings["ADPassword"];  
using (var context = new PrincipalContext(ContextType.Domain, ADServerName, ADusername, ADpassword))  

so my question is can we connect to this using LDAP string? Thanks

Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,191 Reputation points Microsoft External Staff
    2022-10-13T02:54:27.06+00:00

    Hi @john john ,
    Yes,you can do that.
    You can pass the LDAP string via DirectoryEntry.
    https://learn.microsoft.com/en-us/dotnet/api/system.directoryservices.directoryentry.-ctor?view=windowsdesktop-7.0#system-directoryservices-directoryentry-ctor(system-string-system-string-system-string
    You can do this:

    var directoryEntry = new DirectoryEntry("LDAP://***");  
    directoryEntry.Username = "***";  
    directoryEntry.Password = "***";  
    

    Then you can query "AD" using the DirectorySearcher.

    var directorySearcher = new DirectorySearcher(directoryEntry);  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.