0

所以我需要序列化一个令牌以通过 Azure 中的队列 + NetMessageBinding 发送到服务。令牌来自 ACS,经过一番折腾后,我得到了 Saml2SecurityToken,我可以将其写入字符串:

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

但是,如果我然后尝试从tokenString类似的重新创建 Saml2SecurityToken :

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

或者如果我使用

securityToken = handlers.ReadToken(tokenString);

那么 securityToken 始终为空。

我错过了什么?我怎样才能序列化我的令牌,以便我可以通过 wcf 将它发送到另一个服务?

4

1 回答 1

1

D'oh,这可能是一些编码问题,好像我使用它获取字符串然后一切正常:

var saml2SecurityToken = securityToken as Saml2SecurityToken;

var stringBuilder = new StringBuilder();
using (var xmlWriter = new XmlTextWriter(new StringWriter(stringBuilder, CultureInfo.InvariantCulture)))
{
    handlers.WriteToken(xmlWriter, saml2SecurityToken);
}
于 2013-04-09T07:39:35.463 回答