Reputation: 8492
I'm trying to get a list of all upnsuffixes from AD using C#.
I tried this with no success
public static List<string> GetuPNSuffixes()
{
DirectoryEntry partitions = new DirectoryEntry("LDAP://xxxxx.com/CN=Partitions,CN=Configuration,DC=xxxxx,DC=com", "user", "pass");
DirectorySearcher searcher = new DirectorySearcher(partitions);
searcher.PropertiesToLoad.Add("uPNSuffixes");
List<string> suffixes = new List<string>();
foreach (SearchResult sr in searcher.FindAll())
{
foreach (string pn in sr.Properties.PropertyNames)
{
if (pn == "upnsuffixes")
{
suffixes.Add(sr.Properties[pn].ToString());
}
}
}
return suffixes;
}
This gives me a System.DirectoryServices.DirectoryServicesCOMException: There is no such object on the server
error. I guess because it doesn't like my ldap string. The account I'm authenticating with is a domain admin and I'm using similar code in other places so the login is definitely correct. Maybe the CN=Partitions,CN=Configuration
part is wrong?
I would hope there is a better way to do this without the nested loops. Just trying to get a list of the upnsuffixes.
Also tried this and got the same DirectoryServicesCOMException error:
public static string GetuPNSuffixes()
{
DirectoryEntry entry = new DirectoryEntry("LDAP://xxxxx.com/CN=Partitions,CN=Configuration,DC=xxxxx,DC=com", "user", "pass");
return entry.Properties["upnSuffixes"].ToString();
}
So I guess I'm doing something wrong here with the LDAP string there.
Upvotes: 0
Views: 2006
Reputation: 8492
Was able to pull the list of UPN Suffixes with this:
public static List<string> GetuPNSuffixes()
{
//add root domain
List<string> suffixList = new List<string>();
suffixList.Add(Domain.GetCurrentDomain().Name);
//get the list of alternate domains
DirectoryEntry rootDSE = new DirectoryEntry(@"LDAP://RootDSE");
string context = rootDSE.Properties["configurationNamingContext"].Value.ToString();
DirectoryEntry partition = new DirectoryEntry(@"LDAP://CN=Partitions," + context);
foreach (string suffix in partition.Properties["uPNSuffixes"])
{
suffixList.Add(suffix);
}
return suffixList;
}
Upvotes: 1