Skip to content

Instantly share code, notes, and snippets.

@edewit
Created August 26, 2015 10:04
Show Gist options
  • Select an option

  • Save edewit/85dc6834bfed857a1df9 to your computer and use it in GitHub Desktop.

Select an option

Save edewit/85dc6834bfed857a1df9 to your computer and use it in GitHub Desktop.

url: http://localhost:8080/auth/realms/saml-demo/protocol/saml?SAMLRequest=jVJdT8IwFP0rS99L9wHCGkaCECMJ6gLogy%2BmbHfSpGtnb4fy7x2bRHwQfWtuz7n3nHPvGEWpKj6t3U6v4K0GdN5HqTTy9iMhtdXcCJTItSgBucv4enq35GHP55U1zmRGkY5yGSwQwTppNPGmp%2BfMaKxLsGuwe5nB42qZkJ1zFWdMmUyonUHHR%2F7IZ1BWyhwAGPHmjUapxZH%2FG1o0dpgFoUpkRx80h9Kwk9y2RLwbYzNojSekEAqBeIt5Qhbzl2EeFIUQWxoXeUT7IipoHAcBjYPM3%2FbzUTgE0YAxbTzJPXzTEWtYaHRCu4SEfjCg%2FoiGVxs%2F5lHE%2Fag3iMJn4qVfQq6lzqV%2BvZzbtgMhv91sUpo%2BrDfEewKLrf0GQCbjox%2FeDrdny%2FvvOiZ%2FJD5mZ%2F27YRW%2Fbxou5qlRMjt4U6XM%2B6zJ2zVZOFtDG24p3GUJx4rMadFCubNCowTtCJt0I3%2Be5eQT

gzip inflate and base64 decode:

<?xml version="1.0" encoding="UTF-8"?>
<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns="urn:oasis:names:tc:SAML:2.0:assertion" AssertionConsumerServiceURL="http://localhost:8080/employee/" Destination="http://localhost:8080/auth/realms/saml-demo/protocol/saml" ForceAuthn="false" ID="ID_7d1ffaab-9fd3-4a3f-9911-91c0b4d827ea" IsPassive="false" IssueInstant="2015-08-26T09:33:03.532Z" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Version="2.0">
   <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">http://localhost:8080/employee/</saml:Issuer>
   <samlp:NameIDPolicy AllowCreate="true" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient" />
</samlp:AuthnRequest>
@edewit
Copy link
Author

edewit commented Aug 27, 2015

private string GenerateSAMLRequest(string xml)
{
    var saml = string.Format(xml, Guid.NewGuid());
    var bytes = Encoding.UTF8.GetBytes(saml);
    using (var output = new MemoryStream())
    {
        using (var zip = new DeflateStream(output, CompressionMode.Compress))
        {
            zip.Write(bytes, 0, bytes.Length);
        }
        var base64 = Convert.ToBase64String(output.ToArray());
        return WebUtility.UrlEncode(base64);
    }
}

private string DecodeSAMLResponse(string response)
{
    using (var input = new MemoryStream(Convert.FromBase64String(WebUtility.UrlDecode(response))))
    {
        using (var unzip = new DeflateStream(input, CompressionMode.Decompress))
        {
            using (var reader = new StreamReader(unzip, Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment