2016-07-25 85 views
1

所以我在這裏有一個非常類似的問題。 SOAP KeyInfo values在SOAP請求中添加KeyInfo參考

我想在KeyInfo中添加引用,但似乎無法找到通過代碼執行的方法。

這裏是預期的輸出應該是什麼:

<KeyInfo> 
    <wsse:SecurityTokenReference> 
     <wsse:Reference URI="#SecurityTest" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/> 
    </wsse:SecurityTokenReference> 
</KeyInfo> 

而且我確實有這個上面它嘗試引用自:

<wsse:BinarySecurityToken ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" 
     EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" 
     xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
     wsu:Id="SecurityTest">Base64CertStuffBlahblah 
</wsse:BinarySecurityToken> 

在創建的密鑰信息部分將盡力只允許我插入一個項目,像一個鍵,填寫這部分,但我只是想參考。這段代碼是我一直在努力的,但目前還沒有創造出我想要的。

//This creates a X509 clause but it's as far as I've got. 
//The "keyInfoData" needs to be of a different type to allow custom reference? 
var signer = new SignedXmlWithId(doc) {SigningKey = Key}; 
KeyInfo keyInfo = new KeyInfo(); 
KeyInfoX509Data keyInfoData = new KeyInfoX509Data(); 
keyInfoData.AddCertificate(cert); 
keyInfo.AddClause(keyInfoData); 
signer.KeyInfo = keyInfo; 

感謝您的期待,任何幫助將不勝感激。

回答

2

因此,這段代碼讓我可以將我想要的內容添加到KeyInfo部分。

KeyInfo keyInfo = new KeyInfo(); 
XmlElement x = doc.CreateElement("wsse","SecurityTokenReference", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); 
XmlElement y = doc.CreateElement("wsse","Reference", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); 
y.SetAttribute("URI","#SecurityTest"); 
y.SetAttribute("ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"); 
x.AppendChild(y); 
var keyInfoData = new KeyInfoNode(x); 
keyInfo.AddClause(keyInfoData); 
signer.KeyInfo = keyInfo; 

這將產生以下結果:

<KeyInfo> 
    <wsse:SecurityTokenReference> 
     <wsse:Reference URI="#SecurityTest" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" /> 
    </wsse:SecurityTokenReference> 
</KeyInfo> 

這似乎並沒有解決我的問題,雖然現在SOAP「看起來」是正確的。也許它會幫助別人。