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

C# 如何使用System.DirectoryServices.Protocols连接到RootDSE和/或检索HighestCommittedSN?,c#,.net,directoryservices,adlds,C#,.net,Directoryservices,Adlds,使用System.DirectoryServices,可以通过以下方式获得最高级别的委员会: using(DirectoryEntry entry = new DirectoryEntry("LDAP://servername:636/RootDSE")) { var usn = entry.Properties["highestCommittedUSN"].Value; } 但是,我需要使用System.DirectoryServices.Protocols(不利用ADSI)从远程

使用System.DirectoryServices,可以通过以下方式获得最高级别的委员会:

using(DirectoryEntry entry = new DirectoryEntry("LDAP://servername:636/RootDSE"))
{
     var usn = entry.Properties["highestCommittedUSN"].Value;
}
但是,我需要使用System.DirectoryServices.Protocols(不利用ADSI)从远程ADLDS获取此信息。以下是我尝试执行的简化代码示例:

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()中可能有错误,但该代码在许多其他地方被调用以连接到目录,并且从未出错


有什么想法吗?

谢谢你的想法,齐洛。显然,要连接到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];
}

我希望这能在将来为其他人节省一些时间。

谢谢你的想法,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];
}

我希望这能在将来为其他人节省一些时间。

尝试用域组件替换RootDSE。ie:dc=域,dc=com@Zilog-谢谢你的主意。不过,我刚刚找到了解决办法。您必须为根容器指定“null”(其中我有“RootDSE”)。不过,我要到明天才能发布我自己问题的答案,所以我会在明天发布。是的,和SearchScope.base尝试用域组件替换RootDSE。ie:dc=域,dc=com@Zilog-谢谢你的主意。不过,我刚刚找到了解决办法。您必须为根容器指定“null”(其中我有“RootDSE”)。不过,我要到明天才能发布我自己问题的答案,所以我会在那时发布。是的,还有SearchScope.base谢谢。我花了很多时间寻找这个解决方案。我已经用Java实现了它(它使用一个空字符串来表示根),但是还没有找到与.NET等效的。谢谢。我花了很多时间寻找这个解决方案。我已经用Java实现了它(它使用一个空字符串来表示根),但还没有找到与.NET等效的。