visual tutorial studio ejemplo consumir c# .net wcf

c# - tutorial - Cómo detener temporalmente los errores de certificado con los servicios de WCF



wcf tutorial (5)

Estoy probando un lanzamiento anticipado de un servicio web de WCF que he creado. En el lado del cliente cuando uso VS para ''agregar referencia de servicio'', todo funciona.

Pero cuando trato de usar el servicio obtengo el error,

Could not establish trust relationship for the SSL/TLS secure channel with authority **

Donde las estrellas representan la dirección IP del servidor.

De todos modos, en el servidor hay un certificado de seguridad, pero se ha generado solo para las pruebas, por lo que no me preocupan los errores de certificado por el momento.

En el lado del cliente, se ha generado un app.config para mí,

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="BindingName" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Transport"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="***************" binding="wsHttpBinding" bindingConfiguration="BindingName" contract="***************" name="BindingName"> <identity> <servicePrincipalName value="***************" /> </identity> </endpoint> </client> </system.serviceModel> </configuration>

Entonces, ¿qué ajustes necesito cambiar para ignorar temporalmente los errores de certificado?


Modificar web.config funcionó para mí

Lo hice usando la respuesta de Steve Ellinger y algo de Google. Esencialmente, tuve que:

  • decirle al administrador de las conexiones HTTP que use el certificado sin hacer coincidir el nombre del certificado con el nombre de host del servidor y sin verificar si el certificado ha sido revocado
  • modificar el comportamiento del punto final en el lado del cliente para desactivar la validación del certificado

Aquí están los fragmentos de web.config ...

<configuration> <system.net> <settings> <servicePointManager checkCertificateName="false" checkCertificateRevocationList="false" /> </settings> </system.net> <system.serviceModel> <client> <endpoint ... behaviorConfiguration="DisableServiceCertificateValidation" /> </client> <behaviors> <endpointBehaviors> <behavior name="DisableServiceCertificateValidation"> <clientCredentials> <serviceCertificate> <authentication certificateValidationMode="None" revocationMode="NoCheck" /> </serviceCertificate> </clientCredentials> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> </configuration>


<configuration> <system.net> <settings> <servicePointManager checkCertificateName="false" checkCertificateRevocationList="false" /> </settings> </system.net> </configuration>

Esto funciona para mí Gracias


También puede anular con este delineador.

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;

Simplemente péguelo en el constructor de cliente WCF generado en Reference.cs

[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] public partial class WebQuoteServiceClient : System.ServiceModel.ClientBase<Corp.Legal.Webservices.ServiceReference1.IWebQuoteService>, Corp.Legal.Webservices.ServiceReference1.IWebQuoteService { public WebQuoteServiceClient() { ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true; }


Establezca CertificatePolicy PRIOR para inicializar su servicio WCF en el cliente. Así es como (solo haga una llamada al método SetCertificatePolicy () una vez)

/// <summary> /// Sets the cert policy. /// </summary> private static void SetCertificatePolicy() { ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate; } /// <summary> /// Certificate validation callback /// </summary> private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { if (error == SslPolicyErrors.None) { return true; // already determined to be valid } switch (cert.GetCertHashString()) { // thumbprints/hashes of allowed certificates (uppercase) case "066CF9CAD814DE2097D368F22D3A7D398B87C4D6": case "5B82C96685E3A20079B8CE7AFA32554D55DB9611": Debug.WriteLine("Trusting X509Certificate ''" + cert.Subject + "''"); return true; default: return false; } }