How to access nativeGuid instead of objectguid?

Oct 28, 2013 at 7:51 PM
Hi Hatter, been using your library for about 2 year now, love it!

I was wondering how I can access the nativeguid as opposed to the objectguid? I am trying to sync up some old apps that stored userIds in a database based on their Ad nativeguid, and currently my AD API uses objectguid, so i was trying to get the native guid as well, I've tried this:
public PersonClassMap()
        {
            NamingContext(LdapConnectionStrings.UillinoisUserPath);
            ObjectClass("person");
            DistinguishedName(x => x.DistinguishedName);
            Map(x => x.FirstName).Named("givenname");
            Map(x => x.LastName).Named("sn");
            Map(x => x.Department).Named("department");
            Map(x => x.Address).Named("postaladdress");
            Map(x => x.Id).Named("objectguid").StoreGenerated();
            Map(x => x.NativeGuid).Named("nativeguid");
            Map(x => x.TelephoneNumber).Named("telephoneNumber");
            Map(x => x.Title).Named("title");
            Map(x => x.MemberOf).Named("memberof");
            Map(x => x.Username).Named("samaccountname");
            Map(x => x.UiNumber).Named("uiuceduuin");
            Map(x => x.MiddleName).Named("middlename");
        }
Current NativeGuid in my object is a string, according to this http://stackoverflow.com/questions/1644989/difference-between-nativeguid-and-guid-in-active-directory, it should be a string. But I've also tried setting it to a guid, trying add/removing .StoreGenerated() to the end, and every time the value shows up as null or an empty guid.

How can I retreive the nativeGuid as opposed to the objectGuid?
Coordinator
Oct 30, 2013 at 3:00 AM
Hi, I'm glad to hear you're still getting use out of the library. It's hard to believe it's been that long!

Looking over that answer, it looks like they are the same value, just in a different endian format. I dug around and turned up this post that explains the difference and how to convert them:
private static Guid ConvertFromNative(string native)
{
    var guid = new Guid(native).ToByteArray();
    var first = guid.Take(4).Reverse().ToList();
    var second = guid.Skip(4).Take(2).Reverse().ToList();
    var third = guid.Skip(6).Take(2).Reverse().ToList();
    var last = guid.Skip(8).ToList();

    var list = new List<byte>();
    list.AddRange(first);
    list.AddRange(second);
    list.AddRange(third);
    list.AddRange(last);
    var finalGuid = new Guid(list.ToArray());

    return finalGuid;
}

private static string ConvertToNative(Guid guid)
{
    var objectguid = guid.ToByteArray();
    var first = objectguid.Take(4).Reverse().ToList();
    var second = objectguid.Skip(4).Take(2).Reverse().ToList();
    var third = objectguid.Skip(6).Take(2).Reverse().ToList();
    var last = objectguid.Skip(8).ToList();

    var list = new List<byte>();
    list.AddRange(first);
    list.AddRange(second);
    list.AddRange(third);
    list.AddRange(last);
    var finalGuid = new Guid(list.ToArray());

    return finalGuid.ToString("N");
}
Since it's a 100% derived value, you shouldn't have to map it and instead just handle it in your class. You can probably clean up those methods some more as well. I was just curious to see if I could get it to work. Also, I only tested it on a few directory objects so you may want to run it through a few more tests.
Marked as answer by sah302 on 10/30/2013 at 8:31 AM
Coordinator
Oct 30, 2013 at 3:15 AM
Oh, I forgot to mention that you'll need to keep ObjectGuid and just convert it to NativeGuid when necessary. From what I can tell NativeGuid is something that S.DS exposes and isn't available in S.DS.P.
Oct 30, 2013 at 3:31 PM
Rightio, excellent Mr. Hatter!