gpt4 book ai didi

wcf - 在 WCF customBinding 中同时接受 UsernameToken 和 BinarySecurityToken

转载 作者:行者123 更新时间:2023-12-04 18:19:19 24 4
gpt4 key购买 nike

我正在使用 customBinding 端点构建 WCF Web 服务,并且在接受另一方发送给我的 WS-Security header 时遇到问题。我们都遵循英国国家卫生服务局制定的规范,因此我无法修改要求。
<wsse:Security>的基本结构根据规范, header 应如下所示:

<wsse:Security>
<wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurityutility-1.0.xsd" wsu:Id="6CCF6A2B-11A6-11DF-86D1-236A99759561" >
<wsu:Created>2012-06-12T09:00:00Z</wsu:Created>
<wsu:Expires>2012-06-12T09:15:00Z</wsu:Expires>
</wsu:Timestamp>
<wsse:UsernameToken>
<wsse:Username>SomeUsername</wsse:Username>
</wsse:UsernameToken>
<wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsssoap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-
200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="30b91ede-35c2-11df-aac9-97f155153931 ">xxx...</wsse:BinarySecurityToken>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#RSA-SHA1" />
<Reference URI="#6CCF6A2B-11A6-11DF-86D1-236A99759561" />
<Transforms>
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>xxx...</DigestValue>
</SignedInfo>
<SignatureValue>xxx...</SignatureValue>
<KeyInfo>
<wsse:SecurityTokenReference>
<wsse:Reference URI="#30b91ede-35c2-11df-aac9-97f155153931 "/>
</wsse:SecurityTokenReference>
</KeyInfo>
</Signature>
</wsse:Security>

在我的 Web 服务中,我尝试使用以下绑定(bind):
<customBinding>
<binding name="wsHttpSoap11" >
<textMessageEncoding messageVersion="Soap11WSAddressingAugust2004" />
<security authenticationMode="MutualCertificate">
</security>
<httpTransport/>
</binding>
</customBinding>

(我使用 customBinding 的原因是我必须同时支持 WS-Addressing WS-Security 超过 SOAP 1.1 ,并听取了 this answer 的建议.)

如果我通过 Fiddler 运行示例请求,我的 WCF 跟踪中会出现以下错误:

Cannot find a token authenticator for the 'System.IdentityModel.Tokens.UserNameSecurityToken' token type. Tokens of that type cannot be accepted according to current security settings.



我相信这是因为它无法验证 <UsernameToken> .如果我将绑定(bind)安全更改为:
<security authenticationMode="UserNameForCertificate">

然后我得到这个错误:

Cannot find a token authenticator for the 'System.IdentityModel.Tokens.X509SecurityToken' token type. Tokens of that type cannot be accepted according to current security settings.



我相信这是因为它现在无法验证 <BinarySecurityToken> !

因此,问题是:
  • 我对错误消息原因的假设是否正确
    (它在当前配置中只能处理一个 token )?
  • 如何将其配置为接受两个 token ?

  • 更新

    感谢@Yaron,我现在添加了自定义绑定(bind)扩展和 用户名安全 token X509SecurityToken 正在验证。

    但是,它现在在验证 XML 签名的阶段失败了。 HTTP 响应中返回的异常是:

    Message security verification failed.



    如果我深入研究服务跟踪查看器中的堆栈跟踪,我会看到:

    System.Security.Cryptography.CryptographicException...

    The signature verification failed.

    at System.IdentityModel.SignedXml.VerifySignature(HashAlgorithm hash, AsymmetricSignatureDeformatter deformatter) at System.IdentityModel.SignedXml.StartSignatureVerification(SecurityKey verificationKey)...



    谁能帮我弄清楚为什么会这样?我现在有点迷路了。我尝试使用 some sample code尝试手动验证签名,但它说签名无效。在我回到供应商处之前,我如何确定它是否存在?这是应该工作的东西吗?我们应该在某个地方共享一些证书吗?

    最佳答案

    您将需要从代码创建绑定(bind)。

            var b = new CustomBinding();
    var sec = (AsymmetricSecurityBindingElement)SecurityBindingElement.CreateMutualCertificateBindingElement(MessageSecurityVersion.WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10);
    sec.EndpointSupportingTokenParameters.Signed.Add(new UserNameSecurityTokenParameters());
    sec.MessageSecurityVersion =
    MessageSecurityVersion.
    WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10;
    sec.IncludeTimestamp = true;
    sec.MessageProtectionOrder = System.ServiceModel.Security.MessageProtectionOrder.EncryptBeforeSign;

    b.Elements.Add(sec);
    b.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8));
    b.Elements.Add(new HttpTransportBindingElement());

    (有些值是估计的,因为我无法通过您的帖子判断您使用的是哪个肥皂版本或是否应用了 ssl)

    您可能稍后运行的另一个问题是您需要在 ServiceContract 属性上有 ProtectionLevel.SignOnly,但这与这个问题无关。

    关于wcf - 在 WCF customBinding 中同时接受 UsernameToken 和 BinarySecurityToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10999924/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com