Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for a objectGUID in AD

I'm using the Active Directory Explorer from Mark Russinovich. It is a great tool.

I'm using it to navigate active directory to make sure my program that uses DirectorySearcher from .NET returns correct data.

Something happens though, when I try to search inside my program with DirectorySearcher for objectGUID, if I pass in the actual GUID as a string it doesn't return anything, where as if I use Active Directory Explorer, when I add

objectGuid with value f8d764ff-9a6a-418e-a641-b6f99661a8d5, its search clause becomes: (objectGUID=\FFd\D7\F8j\9A\8EA\A6A\B6\F9\96a\A8\D5*)

How do I do this for directorySearcher in my program, I'm guessign it's an octet string thing, but I can't figure it out.

like image 726
Matt Avatar asked Oct 09 '09 19:10

Matt


1 Answers

The forums accompanying the excellent The .NET Developer's Guide to Directory Services Programming (Joe Kaplan / Ryan Dunn) is an excellent source for information like this.

Check out this thread here entitled Find the object using objectGuid property, which shows how you can convert a "regular" GUID to the S.DS "OctetString" format.

internal string ConvertGuidToOctetString(string objectGuid)
{
   System.Guid guid = new Guid(objectGuid);
   byte[] byteGuid = guid.ToByteArray();

   string queryGuid = "";

   foreach (byte b in byteGuid)
   {
       queryGuid += @"\" + b.ToString("x2");
   }

   return queryGuid; 
}

This could be slightly optimized by using a StringBuilder instead of consecutively concatenating together a string - but it seems fairly straightforward otherwise.

Hope this helps.

Marc

like image 75
marc_s Avatar answered Sep 22 '22 18:09

marc_s