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

时间:2013-08-22 14:50:44

标签: mobile encoding active-directory ldap xamarin

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

使用Novell,我可以使用

根据域,用户名和密码对用户进行身份验证
LdapConnection.bind(username, password);

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

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

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

System.Convert.FromBase64String 

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

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 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:7)

ObjectGUID 是二进制字符串(或八位字符串),因此当您尝试显示该值时,您可能会看到一些随机的无意义字符。

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

如果您不介意阅读php示例,请查看php中转换的my implementation

这是有问题的功能。它期望从服务器返回的原始二进制形式的$ 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;
}

答案 2 :(得分:1)

在Novell库中,objectGUID是一个SByte数组,需要在转换为Byte之前转换为GUID数组。您还需要获取ByteValue的{​​{1}},而不是objectGUID

StringValue

答案 3 :(得分:0)

对我来说,解决方案是以下列方式实例化GUID:

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;
}