1
我有些網站在連接到第三方SOAP WS時失敗了。當第三方WS有很多請求時,這種情況發生得最多。所以它不會每次都失敗,只有一些時候 - 而且大多數時候對第三方ws有很多要求。獲取「無法創建SSL/TLS安全通道」
它已在我們的使用IIS Crypto的內部服務器上修復。所有禁用的複選框之前都被檢查過。
但是在Azure WebApp上,我們無法控制這些設置 - 任何想法該怎麼辦?
繼承人是結合多向第三方肥皂WS的代碼:
public static CustomBinding GetDefaultBinding()
{
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
AsymmetricSecurityBindingElement securityElement = new AsymmetricSecurityBindingElement();
securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12;
securityElement.InitiatorTokenParameters = new X509SecurityTokenParameters(X509KeyIdentifierClauseType.IssuerSerial, SecurityTokenInclusionMode.Never);
securityElement.RecipientTokenParameters = new X509SecurityTokenParameters(X509KeyIdentifierClauseType.IssuerSerial, SecurityTokenInclusionMode.Never);
securityElement.ProtectTokens = true;
securityElement.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt;
securityElement.RequireSignatureConfirmation = true;
securityElement.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
securityElement.EnableUnsecuredResponse = true;
securityElement.IncludeTimestamp = true;
securityElement.SetKeyDerivation(false);
securityElement.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic128Rsa15;
securityElement.EndpointSupportingTokenParameters.SignedEncrypted.Add(new UserNameSecurityTokenParameters());
securityElement.AllowSerializedSigningTokenOnReply = true;
CustomBinding myBinding = new CustomBinding();
myBinding.Elements.Add(securityElement);
TextMessageEncodingBindingElement element = new TextMessageEncodingBindingElement(MessageVersion.Soap11WSAddressing10, Encoding.UTF8);
element.ReaderQuotas.MaxStringContentLength = int.MaxValue;
element.ReaderQuotas.MaxDepth = int.MaxValue;
element.ReaderQuotas.MaxArrayLength = int.MaxValue;
element.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
element.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
myBinding.Elements.Add(element);
HttpsTransportBindingElement httpsBindingElement = new HttpsTransportBindingElement();
httpsBindingElement.RequireClientCertificate = true;
httpsBindingElement.MaxBufferPoolSize = int.MaxValue;
httpsBindingElement.MaxBufferSize = int.MaxValue;
httpsBindingElement.MaxReceivedMessageSize = int.MaxValue;
httpsBindingElement.KeepAliveEnabled = false;
httpsBindingElement.AllowCookies = false;
myBinding.Elements.Add(httpsBindingElement);
myBinding.CloseTimeout = new TimeSpan(0, 10, 0);
myBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
myBinding.SendTimeout = new TimeSpan(0, 10, 0);
return myBinding;
}
private void ConfigureClientCredentials(ClientCredentials cc)
{
if (cc == null) return;
cc.UserName.UserName = Options.WebserviceUsername;
cc.UserName.Password = Options.AuthPassword;
cc.ClientCertificate.Certificate = Options.ClientCertificate;
cc.ServiceCertificate.DefaultCertificate = Options.IbaCertificate;
cc.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
}
private void ConfigureEndPoint(ServiceEndpoint endpoint)
{
endpoint.Contract.ProtectionLevel = ProtectionLevel.EncryptAndSign;
endpoint.EndpointBehaviors.Add(new CustomEndpointBehavior());
}
對不起 - 我剛剛更新了我的答案:-) – pajzo
這很快 - 太棒了。最後一個提示:我想你可能想閱讀Clean Code(Robert Martin的偉大書籍)。你的代碼可能會從中受益。 – GhostCat