开发者

Decode SAML 2.0 64Base string

I've been developing a SAML 2.0 SSO. I got a problem in decoding 64Base string to convert it into an xml then read the content later on.

Below is the code I've been using:

Protected Function uf_getIDFromSAMLRequest(ByVal as_samlRequest As String) As String
        Dim ms As New MemoryStream(Convert.FromBase64String(as_samlRequest))
        Dim decompress As New DeflateStream(ms, CompressionMode.Decompress)
        Dim xmlMessageBytes As Byte() = New Byte() {}
        Dim m1 As New MemoryStream(xmlMessageBytes)
        decompress.CopyTo(m1)
        Dim document As New XmlDocument() With { _
         .PreserveWhitespace = True _
        }
        document.LoadXml(Encoding.UTF8.GetString(xmlMessageBytes))
        Return document.DocumentElement.Attributes("ID").InnerXml.ToString()
End Function

Below is the base64 string encrypted saml request:

PHNhbWxwOkF1dGhuUmVxdWVzdCB4bWxuczpzYW1scD0idXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOnByb3RvY29sIiBJRD0iX2JlYzQyNGZhNTEwMzQyODkwOWEzMGZmMWUzMTE2ODMyN2Y3OTQ3NDk4NCIgVmVyc2lvbj0iMi4wIiBJc3N1ZUluc3RhbnQ9IjIwMDctMTItMTBUMTE6Mzk6MzRaIiBGb3JjZUF1dGhuPSJmYWxzZSIgSXNQYXNzaXZlPSJmYWxzZSIgUHJvdG9jb2xCaW5kaW5nPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6YmluZGluZ3M6SFRUUC1QT1NUIiBBc3NlcnRpb25Db25zdW1lclNlcnZpY2VVUkw9Imh0dHBzOi8vaWZpbS1xYS5wcnVkZW50aWFsLmNvbS9zcHMvaW50LW1pY3JvbnN5c3RlbXMvc2FtbDIwL2xvZ2luIj4NCiAgICA8c2FtbDpJc3N1ZXIgeG1sbnM6c2FtbD0idXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOmFzc2VydGlvbiI+DQogICAgICAgIHVybjptYWNlOmZlaWRlLm5vOnNlcnZpY2VzOm5vLmZlaWRlLm1vb2RsZQ0KICAgIDwvc2FtbDpJc3N1ZXI+DQogICAgPHNhbWxwOk5hbWVJRFBvbGljeSB4bWxuczpzYW1scD0idXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOnByb3RvY29sIiBGb3JtYXQ9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDpuYW1laWQtZm9ybWF0OnBlcnNpc3RlbnQiIFNQTmFtZVF1YWxpZmllcj0ibW9vZGxlLmJyaWRnZS5mZWlkZS5ubyIgQWxsb3dDcmVhdGU9InRydWUiIC8+DQogICAgPHNhbWxwOlJlcXVlc3RlZEF1dGhuQ29udGV4dCB4bWxuczpzYW1scD0idXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOnByb3RvY29sIiBDb21wYXJpc29uPSJleGFjdCI+DQogICAgICAgIDxzYW1sOkF1dGhuQ29udGV4dENsYXNzUmVmIHhtbG5zOnNhbWw9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphc3NlcnRpb24iPg0KICAgICAgICAgICAgdXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOmFjOmNsYXNzZXM6UGFzc3dvcmRQcm90ZWN0ZWRUcmFuc3BvcnQNCiAgICAgICAgPC9zYW1sOkF1dGhuQ29udGV4dENsYXNzUmVmPg0KICAgIDwvc2FtbHA6UmVxdWVzdGVkQXV0aG5Db250ZXh0Pg0KPC9zYW1scDpBdXRoblJlcXVlc3Q+

I got an exception: "Found invalid data while decoding." in using the above code.

While testing this encrypted saml request using SAML 2.0 Debugger it should read as:

<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="_bec424fa5103428909a30ff1e31168327f79474984" Version="2.0" IssueInstant="2007-12-10T11:39:34Z" ForceAuthn="false" IsPassive="false" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" AssertionConsumerServiceURL="https://ifim-qa.prudential.com/sps/int-micronsystems/saml20/login">
    <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
        urn:mace:feide.no:services:no.feide.moodle
    </saml:Issuer>
    <samlp:NameIDPolicy xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:persistent" SPNameQualifier="moodle.bridge.feide.no" AllowCreate="true" />
    <samlp:RequestedAuthnContext xmlns:samlp="urn:oasi开发者_运维技巧s:names:tc:SAML:2.0:protocol" Comparison="exact">
        <saml:AuthnContextClassRef xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
            urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport
        </saml:AuthnContextClassRef>
    </samlp:RequestedAuthnContext>
</samlp:AuthnRequest>

THanks for the help in advance!


Ok, I got it work using the following code:

Protected Function uf_getIDFromSAMLRequest(ByVal as_samlRequest As String) As String
        Dim xmlMessageBytes As Byte() = Convert.FromBase64String(as_samlRequest)
        Dim document As New XmlDocument() With { _
         .PreserveWhitespace = True _
        }
        document.LoadXml(Encoding.UTF8.GetString(xmlMessageBytes))
        Return document.DocumentElement.Attributes("ID").InnerXml.ToString()
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜