列出Active Directory中的所有UPN后缀 [英] List all UPN Suffixes from Active Directory
问题描述
我正在尝试使用C#从AD中获取所有upnsuffixes的列表。
I'm trying to get a list of all upnsuffixes from AD using C#.
我尝试失败了
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;
}
这给了我一个 System.DirectoryServices.DirectoryServicesCOMException:服务器上没有这样的对象
错误。我猜是因为它不喜欢我的ldap字符串。我要通过身份验证的帐户是域管理员,并且我在其他地方使用了类似的代码,因此登录名绝对正确。也许 CN = Partitions,CN = Configuration
部分错了?
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?
我希望有更好的方法做到这一点而无需嵌套循环。只是尝试获取upnsuffixes的列表。
I would hope there is a better way to do this without the nested loops. Just trying to get a list of the upnsuffixes.
也尝试了此操作,并得到了相同的DirectoryServicesCOMException错误:
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();
}
所以我想我在这里用LDAP字符串做错了什么。
So I guess I'm doing something wrong here with the LDAP string there.
推荐答案
能够通过以下方式提取UPN后缀列表:
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;
}
这篇关于列出Active Directory中的所有UPN后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!