2013-04-08 57 views
0

所以我需要序列化一個令牌通過Azure中的隊列+ NetMessageBinding發送到服務。令牌來自ACS和一些jiggery pokery後,我有Saml2SecurityToken,我可以寫一個字符串:爲什麼Saml2SecurityHandler不會使用ReadToken從剛剛使用WriteToken創建的字符串創建令牌?

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將它發送到其他服務?

回答

1

D'哦,它可能是一些編碼問題,因爲如果我用這個那麼一切都得到字符串工程確定:

var saml2SecurityToken = securityToken as Saml2SecurityToken; 

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