Query by objectGuid not working?

Sep 3, 2009 at 5:52 AM
Edited Sep 3, 2009 at 6:06 AM

Hi All!  I'm hoping someone can help me out.  I have it all hooked up and am querying (rather quickly I might add...thanks for this Bart!) but I am running into an odd behavior when querying by objectGuid.

var q = (from u in ctx.Users
   where (u.Name == "Jane Doe"
   select new { ID = u.Id });
(&(objectClass=userProxyFull)(Name=Jane Doe))
objectGuid:     ab671b0b-8fc8-42af-bf63-abb23a9d7424
Match:            True     <---- All I am doing here is u.ID == new Guid("ab671b0b-8fc8-42af-bf63-abb23a9d7424") to double check

var q = (from u in ctx.Users
  where u.Id == new Guid("AB671B0B-8FC8-42AF-BF63-ABB23A9D7424")
  select new { ID = u.Id });

Just wondering why the second query doesn't return a result, I know it is a valid GUID cause it was just returned in the query above when I searched by name which also means the property name is correct (objectGUID).  I would expect that it would return the Jane Doe user again because I have her GUID and am filtering by that GUID...

Anyone have any idea why this might be???



Oct 16, 2009 at 4:15 PM

I ended up figuring out what the problem was...

After reading this post: http://kjellsj.blogspot.com/2005/03/get-ad-objects-by-guid-using.html

Figured out that the Search Filter will not work with the code found here:

Around Line 907: GetCondition(BinaryExpression e)
Around Line 942: val = val.ToString().Replace("(", "0x28").Replace(")", "0x29").Replace(@"\", "0x5c");

String Guid: B0C670E5-F05A-4748-8F4E-E8567126DD96
Binary Guid:
Search Filter: (objectGUID=\E5\70\C6\B0\5A\F0\48\47\8F\4E\E8\56\71\26\DD\96)

The search filter was being changed to this:


So I used the code in the link above to create the guid as an octet string:

public static string Guid2OctetString(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; 

...and then placed an if/then to detect when I am searching for an objectGuid in the DirectorySource.cs file so that it will leave the "\" in the search filter when appropriate.

Hope this helps someone else...