0

So I need to serialize a token to send to a service via a queue + NetMessageBinding in Azure. The token comes from ACS and after some jiggery pokery I have the Saml2SecurityToken and I can write it to a string:

SecurityTokenHandlerCollection handlers = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection();
var saml2SecurityToken = securityToken as Saml2SecurityToken;
using (var memoryStream = new MemoryStream())
{
    using (var xmlWriter = new XmlTextWriter(memoryStream, Encoding.UTF8))
    {
        handlers.WriteToken(xmlWriter, saml2SecurityToken);
    }

    tokenString = Encoding.UTF8.GetString(memoryStream.GetBuffer());
}

however if I then try and recreate the Saml2SecurityToken from the tokenString like so:

if (tokenString != null)
{
    using (var stringReader = new StringReader(tokenString))
    {
        using (var xmlReader = new XmlTextReader(stringReader))
        {
            securityToken = handlers.ReadToken(xmlReader);
        }
    }
}

or if I use

securityToken = handlers.ReadToken(tokenString);

then securityToken is always null.

What am I missing? How can I go about serialising my token so I can send it via wcf to another service?

1 Answer 1

1

D'oh, it was probably some encoding issue as if I get the string using this then everything works ok:

var saml2SecurityToken = securityToken as Saml2SecurityToken;

var stringBuilder = new StringBuilder();
using (var xmlWriter = new XmlTextWriter(new StringWriter(stringBuilder, CultureInfo.InvariantCulture)))
{
    handlers.WriteToken(xmlWriter, saml2SecurityToken);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.