Sunday, January 11, 2009

Get Active Directory Connection String

1- create new C# windows application
2- Put Text Box on the Form
3- on Form load Event Copy the Below Code:

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
);
textBox1.Text = adsPath;
}
4- don't forget to make using System.DirectoryServices;
- Add Reference for System.DirectoryServices
5- you must run the application On a machine has a domain

5 comments:

  1. Very nice article man.. keep it going and don't stop go go go :)

    ReplyDelete
  2. Thanks a lot. Worked like a charm :-) Always used to get the connection string wrong!

    ReplyDelete
  3. Thanks a lot, nice app ;)

    ReplyDelete
  4. Awesome piece of code! Thank you!

    ReplyDelete
  5. and four years later... it worked like a charm, too! i was skeptical at first when i saw the "rootDSE" thing, but then learned much more about it from the Windows Dev Center site.

    thanks man, your tips and tricks are simple, yet very useful.

    (Antony)

    ReplyDelete