1

Using System.DirectoryServices, one can get the highestCommittedUSN this way:

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

However, I need to get this information from a remote ADLDS using System.DirectoryServices.Protocols, which does not leverage ADSI. Following is a simplified code sample of what I'm attempting to do:

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

Unfortunately this kicks back a "DirectoryOperationException: The distinguished name contains invalid syntax." At first I thought there might be something wrong in GetWin32LdapConnection() but that code is called in numerous other places to connect to the directory and never errors out.

Any ideas?

pwil301
  • 323
  • 4
  • 13
  • 1
    Try to replace RootDSE with domain components. ie: dc=domain,dc=com – Zilog Oct 31 '13 at 01:49
  • @Zilog - Thanks for the idea. I just found the solution, though. You have to specify "null" for the root container (where I have "RootDSE"). I can't post an answer to my own question until tomorrow, though - so I'll put it up then. – pwil301 Oct 31 '13 at 02:06
  • yes, and SearchScope.Base – Zilog Oct 31 '13 at 02:39

1 Answers1

6

Thanks for the idea, Zilog. Apparently to connect to the RootDSE, you have to specify null for the root container. I also switched the filter to objectClass=* and the search scope to "base." Now it works!

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

I hope this saves someone else some time in the future.

pwil301
  • 323
  • 4
  • 13
  • Thanks. I've been spending a lot of time searching for just this solution. I've implemented it in Java (which uses an empty string to indicate root) but haven't been able to find the .NET equivalent. – ke4ktz Oct 16 '14 at 14:59