如何使用System.DirectoryServices.Protocols连接到RootDSE和/或检索highestCommittedUSN?

时间:2013-10-31 01:12:29

标签: c# .net directoryservices adlds

使用System.DirectoryServices,可以通过这种方式获得最高的通信量:

using(DirectoryEntry entry = new DirectoryEntry("LDAP://servername:636/RootDSE"))
{
     var usn = entry.Properties["highestCommittedUSN"].Value;
}

但是,我需要使用System.DirectoryServices.Protocols从远程ADLDS获取此信息,该协议不利用ADSI。以下是我正在尝试做的简化代码示例:

using(LdapConnection connection = GetWin32LdapConnection())
{
     var filter = "(&(highestCommittedUSN=*))";
     var searchRequest = new SearchRequest("RootDSE", filter, SearchScope.Subtree, "highestCommittedUSN");
     var response = connection.SendRequest(searchRequest) as SearchResponse;
     var usn = response.Entries[0].Attributes["highestCommittedUSN"][0];
}

不幸的是,这会引发“DirectoryOperationException:专有名称包含无效语法”。起初我以为GetWin32LdapConnection()可能有问题但是在许多其他地方调用该代码来连接到目录并且永远不会出错。

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

感谢Zilog的想法。显然要连接到RootDSE,您必须为根容器指定null。我还将过滤器切换到objectClass = *,将搜索范围切换为“base”。现在它有效!

using(LdapConnection connection = GetWin32LdapConnection())
{
 var filter = "(&(objectClass=*))";
 var searchRequest = new SearchRequest(null, filter, SearchScope.Base, "highestCommittedUSN");
 var response = connection.SendRequest(searchRequest) as SearchResponse;
 var usn = response.Entries[0].Attributes["highestcommittedusn"][0];
}

我希望将来可以节省别人的时间。