简体   繁体   中英

Object GUID from AD for a user

I have a task to find object guid of and end user. I am using C# to find it. After reading few articles on this topic I used DirectoryEntry class to find out the AD user details. The data from SearchResult object after querying AD is giving me the objectguid but this is not exactly a GUID in .net terms. I had to change it to a proper guid so that I used it in the application. When I searched internet about converting objectguid to a GUID I found out two different ways as outlined below

First one:

DirectoryEntry de = searchResult.GetDirectoryEntry();
Guid userGuid = new Guid(de.NativeGuid);

Second one:

Guid userGuid = new Guid((byte[]) searchResult.Properties["objectguid"][0]);

Both are giving me a GUID but they are different. I dont understand which one to consider. As I have to do it for about 50000 records, the first option is taking me quite a lot of time which is not acceptable in my scenario. Second option is not taking much time but I am not sure if the GUID that is created in this way is fine.

Here my question is

  1. Is user guid that I got in the second option for the AD user the right one to use ?
  2. If the GUID from the first option is the right one is there any way I can find in a better way (with respect the the time it is taking) ?

Thanks for your time.

From the comments,
For case one, a simpler way to get correct GUID is Guid userGuid = searchResult.GetDirectoryEntry().Guid; But second one is preferred as case one will generate more AD access to bind the DirectoryEntry.

Its simply an encoding difference between the two(native vs regular) but the value is the same. ALWAYS avoid hitting the searchResult.GetDirectoryEntry() method if you can. It will literally go and get the full record again from the AD. You wouldn't want to select two fields then go back and select them all when you really only need the two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM