gpt4 book ai didi

c# - WCF over HTTPS 和签署正文

转载 作者:太空狗 更新时间:2023-10-29 22:27:32 26 4
gpt4 key购买 nike

我有一个要连接的 SOAP 服务。它需要通过 https 访问,并且它的正文需要由证书签名。

我试过下面的代码:

<basicHttpBinding>
<binding name="P4Binding" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Certificate" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>

按如下方式设置我的客户端:

P4_ServiceReference.P4PortTypeClient client = new P4_ServiceReference.P4PortTypeClient();

client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
client.ClientCredentials.ServiceCertificate.DefaultCertificate = new X509Certificate2(@"[..].cer");
client.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(@"[..]", "somepass");

甚至更改了我的 Reference.cs 以在 ServiceContractAttribute 和 OperationContractAttribute 上包含 ProtectionLevel=ProtectionLevel.Sign

发生的情况是创建了 wse:security header ,但未对主体进行签名。该服务返回 Element http://schemas.xmlsoap.org/soap/envelope/Body must be signed

我缺少什么才能正确签署正文?

最佳答案

使用 customBinding 而不是 basicHttpBinding 修复了它。

        //Setup custom binding with HTTPS + Body Signing + Soap1.1
CustomBinding binding = new CustomBinding();

//HTTPS Transport
HttpsTransportBindingElement transport = new HttpsTransportBindingElement();

//Body signing
AsymmetricSecurityBindingElement asec = (AsymmetricSecurityBindingElement)SecurityBindingElement.CreateMutualCertificateBindingElement(MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10);
asec.SetKeyDerivation(false);
asec.AllowInsecureTransport = true;
asec.IncludeTimestamp = true;

//Setup for SOAP 11 and UTF8 Encoding
TextMessageEncodingBindingElement textMessageEncoding = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);

//Bind in order (Security layer, message layer, transport layer)
binding.Elements.Add(asec);
binding.Elements.Add(textMessageEncoding);
binding.Elements.Add(transport);

TransportWithMessageCredential 似乎只使用 Transport 来保证安全,而忽略了其他一切。

关于c# - WCF over HTTPS 和签署正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11848919/

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