如何将Active Directory objectGuid转换为可读字符串?

lil*_*s27 8 mobile encoding ldap active-directory xamarin

我正在使用Novell.Directory.Ldap用C#编写的Xamarin移动应用程序.

使用Novell,我可以使用域名,用户名和密码对用户进行身份验证

LdapConnection.bind(username, password);
Run Code Online (Sandbox Code Playgroud)

然后,我使用sAMAccountName相当于提供的用户名的搜索执行搜索.

在所有这一切成功运行之后,我需要获取用户,objectGuid以便我可以查询外部数据库,这些数据库使用guid作为密钥.问题是,当我从后面拿回guid时LdapSearchResults,它以某种方式编码.我无法弄清楚如何获得这个guid的可读字符串表示.

有没有人有这方面的更多信息?我会想象guid是以某种方式编码的,但它是如何编码的,我不知道.我试过了

System.Convert.FromBase64String 
Run Code Online (Sandbox Code Playgroud)

那并没有帮助.我很感谢帮助人员,如果我能发布任何有用的信息,请告诉我.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 12

我不确定Novell库是否以其他方式对其进行编码,但System.DirectoryServices将GUID作为字节数组提供.您可以使用System.Guid结构将其转换为可读字符串:

new Guid((System.Byte[])this.GUID).ToString()
Run Code Online (Sandbox Code Playgroud)


Rob*_*ann 7

的objectGUID是一个二进制字符串(或字节串),那么你很可能看到的是一些随机的无意义的字符,当您试图显示的值.

的objectGUID实际上遵循公认的标准-这是一个UUID版本4.由于我不使用C#,我无法提供工作示例,但是有了这些信息,您应该能够将二进制字符串解码为可读的字符串表示形式,或者至少找到一个有效的代码示例.我强烈怀疑在C#中有一些本地类或库可以与UUID/Guids一起使用.

如果您不介意阅读php示例,请查看在php中的转换实现.

这是有问题的功能.它期望从服务器返回的原始二进制形式的$ guid.

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;
}
Run Code Online (Sandbox Code Playgroud)