How to get Active Directory connection string in C#.NET

How to get Active Directory connection string in C#.NET

First you will have to add a reference for System.DirectoryServices
You must run the application on a machine within the same domain to be able to connect to the Active Directory

using System.DirectoryServices;

private string GetActiveDirectoryConnectionString()
{
  DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");
  using (root)
  {
    string dnc = root.Properties["defaultNamingContext"][0].ToString();
    string server = root.Properties["dnsHostName"][0].ToString();
    string adsPath = String.Format("LDAP://{0}/{1}", server, dnc);
  }
  return adsPath;
}

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *