List all UPN Suffixes from Active Directory

此生再无相见时 提交于 2020-01-06 07:28:10

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/48916187/list-all-upn-suffixes-from-active-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!