Building an LDAP domain path for AD queries in C# .NET
Little task I had to do this morning I thought I'd share.
I'm knocking up something that will do some active directory queries, and the first step was to connect using the LDAP Path. I wanted this to be fairly automated though, and the following code will work out the domain of the local machine and build the LDAP string for you. Might be of use... :)
String DNSDomainName = Dns.GetHostEntry(Dns.GetHostName()).HostName;
private void GetUsersFromAD()
{
String DNSDomainName = Dns.GetHostEntry(Dns.GetHostName()).HostName;
DirectoryEntry de = new DirectoryEntry();
String[] DNSComponents = DNSDomainName.Split('.');
String LDAPPath = "LDAP://OU=Domain";
for (int i = 1; i < DNSComponents.Length; i++)
{
LDAPPath = LDAPPath + ",DC=" + DNSComponents[i].ToString();
}
de.Path = LDAPPath;
}
I'm knocking up something that will do some active directory queries, and the first step was to connect using the LDAP Path. I wanted this to be fairly automated though, and the following code will work out the domain of the local machine and build the LDAP string for you. Might be of use... :)
String DNSDomainName = Dns.GetHostEntry(Dns.GetHostName()).HostName;
private void GetUsersFromAD()
{
String DNSDomainName = Dns.GetHostEntry(Dns.GetHostName()).HostName;
DirectoryEntry de = new DirectoryEntry();
String[] DNSComponents = DNSDomainName.Split('.');
String LDAPPath = "LDAP://OU=Domain";
for (int i = 1; i < DNSComponents.Length; i++)
{
LDAPPath = LDAPPath + ",DC=" + DNSComponents[i].ToString();
}
de.Path = LDAPPath;
}


0 Comments:
Post a Comment
<< Home