How to convert an ObjectGuid Active Directory object into a readable string?

I am using Novell.Directory.Ldap in a Xamarin mobile application written in C #.

Using Novell, I can authenticate a user based on domain, username and password using

 LdapConnection.bind(username, password); 

Then I do a search using sAMAccountName , which is equivalent to the provided username.

After all this, which works successfully, I need to get the objectGuid user objectGuid that I can query external databases that use this guid as a key. The problem is that when I return the guid back to LdapSearchResults , it is somehow encoded. And I can't figure out how to get a readable string representation of this pointer.

Does anyone have more info on this? I would suggest that guid is encoded in some way, but I don’t know how it is encoded. I tried

 System.Convert.FromBase64String 

and it didn’t help. I appreciate the help guys let me know if I can post more information that would be helpful.

 private void Login() { if (LOG.isInfoEnabled()) { LOG.info("Attempting LDAP logon . . ."); if (LOG.isDebugEnabled()) { LOG.debug("Host: " + this.ldapHost); LOG.debug("Port: " + this.ldapPort); LOG.debug("SearchBase: " + this.ldapSearchBase); } } LdapConnection conn = new LdapConnection(); try { conn.Connect(this.ldapHost, this.ldapPort); if (LOG.isDebugEnabled()) { LOG.debug("connected?: " + conn.Connected.ToString()); } } catch (Exception e) { LOG.error("An exception occurred while attempting to connect to AD server!", e); // INFORM USER ABOUT ERROR authError(Resource.String.error_unknown); } if (!string.IsNullOrEmpty(this.editTextUserName.Text) && !string.IsNullOrEmpty(this.editTextPassword.Text)) { // HIDE KEYBOARD var imm = (InputMethodManager)GetSystemService(Context.InputMethodService); imm.HideSoftInputFromWindow(editTextPassword.WindowToken, HideSoftInputFlags.NotAlways); // HIDE 'LOGON' BUTTON WHILE LOGGING ON this.buttonLogin.Visibility = ViewStates.Invisible; try { // PERFORM AUTHENTICATION conn.Bind(this.userName, this.userPassword); if (LOG.isDebugEnabled()) { LOG.debug("conn.Bound?: " + conn.Bound); } if (conn.Bound) { if (LOG.isDebugEnabled()) { LOG.debug("authentication successful"); } string[] name = this.userName.Split('\\'); LOG.debug("name[0]: " + name[0]); LOG.debug("name[1]: " + name[1]); string filter = "(sAMAccountName=" + name[1] + ")"; string guid = ""; LdapSearchResults searchResults = conn.Search( this.ldapSearchBase, // search base LdapConnection.SCOPE_SUB, // search scope filter, // filter null, // attributes false); // attributes only while (searchResults.hasMore()) { LdapEntry nextEntry = null; try { nextEntry = searchResults.next(); guid = nextEntry.getAttribute("objectGUID").StringValue; } catch (LdapException e) { LOG.error("An exception occurred while attempting to get next search result!", e); continue; } } Intent intent = new Intent(this, typeof(DashboardActivity)); intent.PutExtra("guid", guid); StartActivity(intent); } else { // INFORM USER ABOUT ERROR authError(Resource.String.error_auth); } } catch (LdapException ldape) { LOG.error("An exception occurred while attempting to authenticate user credentials!", ldape); // INFORM USER ABOUT ERROR authError(Resource.String.error_auth); } finally { conn.Disconnect(); } } else { conn.Disconnect(); } } 
+4
source share
4 answers

I'm not sure the Novell library encodes it in any other way, but System.DirectoryServices provides a GUID as an array of bytes. You can get this for a readable string using the System.Guid struct:

 new Guid((System.Byte[])this.GUID).ToString() 
+7
source

ObjectGUID is a binary string (or octet string), so what you probably see are some random meaningless characters when you try to display this value.

ObjectGUID actually follows a well-established standard - it's UUID version 4 . Since I do not work with C #, I cannot provide a working example, but with this information you should be able to decode the binary string into a readable string representation, or at least find a working code example. I have a strong suspicion that there will be some native class or library for working with UUID / Guids in C #.

If you don't mind reading a php example, check out my php conversion implementation .

Here is the function in question. He expects $ guid in its original binary form to be returned from the server.

 function _to_p_guid( $guid ) { $hex_guid = unpack( "H*hex", $guid ); $hex = $hex_guid["hex"]; $hex1 = substr( $hex, -26, 2 ) . substr( $hex, -28, 2 ) . substr( $hex, -30, 2 ) . substr( $hex, -32, 2 ); $hex2 = substr( $hex, -22, 2 ) . substr( $hex, -24, 2 ); $hex3 = substr( $hex, -18, 2 ) . substr( $hex, -20, 2 ); $hex4 = substr( $hex, -16, 4 ); $hex5 = substr( $hex, -12, 12 ); $guid = $hex1 . "-" . $hex2 . "-" . $hex3 . "-" . $hex4 . "-" . $hex5; return $guid; } 
+6
source

In the Novell library, objectGUID is an SByte array that must be converted to a Byte array before converting to a GUID . You also need to get ByteValue objectGUID , not StringValue .

 try { nextEntry = searchResults.next(); guid = new Guid((Byte[])(Array)nextEntry.getAttribute("objectGUID").ByteValue); } catch (LdapException e) { LOG.error("An exception occurred while attempting to get next search result!", e); continue; } 
0
source

For me, the solution was to create a GUID as follows:

 internal string GetProperty(SearchResult searchResult, String PropertyName) { string retVal = string.Empty; if (searchResult.Properties.Contains(PropertyName)) { if (PropertyName == "objectguid") { Guid _guid = new Guid((Byte[])(Array)searchResult.Properties[PropertyName][0]); retVal = _guid.ToString(); } else retVal = searchResult.Properties[PropertyName][0].ToString(); } return retVal; } 
0
source

Source: https://habr.com/ru/post/1498410/


All Articles