Created
December 5, 2014 13:25
-
-
Save michaelkc/8bb5a4170ed4ae918948 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IdentityModel; | |
using System.Reflection; | |
using System.Text; | |
using System.Web; | |
namespace Samples | |
{ | |
/// <summary> | |
/// A "DeflatedSaml" token header encoder. DeflatedSaml is a semi-standard, in that the encoding/compression steps are described in the SAML standards. | |
/// Fiddlers TextWizard has built-in support for decoding DeflatedSaml. | |
/// </summary> | |
public class DeflatedSamlTokenHeaderEncoder | |
{ | |
private readonly Encoding _encoding = Encoding.UTF8; | |
private readonly DeflateCookieTransform _deflateCookieTransform; | |
public DeflatedSamlTokenHeaderEncoder() | |
{ | |
_deflateCookieTransform = new DeflateCookieTransform(); | |
} | |
public string Encode(string token) | |
{ | |
// Before it’s sent, the message is deflated, base64-encoded, and URL-encoded, in that order. | |
var bytes = _encoding.GetBytes(token); | |
var deflatedBytes = _deflateCookieTransform.Encode(bytes); | |
var base64String = Convert.ToBase64String(deflatedBytes); | |
var urlEncodedString = HttpUtility.UrlEncode(base64String); | |
return urlEncodedString; | |
} | |
public string Decode(string encodedToken) | |
{ | |
var base64String = HttpUtility.UrlDecode(encodedToken); | |
var deflatedBytes = Convert.FromBase64String(base64String); | |
var bytes = _deflateCookieTransform.Decode(deflatedBytes); | |
var token = _encoding.GetString(bytes); | |
return token; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment