c# - 在 SOAP 请求中添加 KeyInfo 引用

标签 c# xml soap xml-signature

所以我遇到的问题与此处的这篇文章非常相似。 SOAP KeyInfo values

我想在 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>

创建 KeyInfo 部分的每一次尝试都只允许我插入一个项目,如 key ,以填写此部分,但我只想要一个引用。这段代码是我一直在使用的代码,但目前还没有创建我想要的代码。

//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;

感谢您的关注,如有任何帮助,我们将不胜感激。

最佳答案

所以这段代码让我可以将我想要的内容添加到 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 现在“看起来”是正确的,但这似乎并没有解决我的问题。也许它会帮助别人。

关于c# - 在 SOAP 请求中添加 KeyInfo 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38575291/

相关文章:

java - 在 glassfish/soap java webservices 中禁用传输编码分块

c# - C#中html内容的显示

c# - Where 子句中的 LINQ 子查询在 LINQPad 中返回错误

c# - 处理 lambda 表达式

c# - 将单词 TOTAL 添加到 Gridview 的最后一行

xml - JAXB Unmarshalling - 是否可以在不使用模式验证的情况下限制元素的出现?

apache - 无法设置 php_value 'soap.wsdl_cache_dir'

web-services - 模拟 Web 服务响应 |将其中的几个捆绑在一个网络应用程序中

ruby - 使用 Savon 2 发送原始 XML

xml - 错误 #2044 : Unhandled securityError:. 文本=错误 #2048:违反安全沙箱: