2009-04-08 26 views
6

Estoy tratando de obtener el servidor WCF y el cliente se autentican mutuamente utilizando certificados SSL en el nivel de transporte utilizando BasicHttpBinding. Así es como se está creado el servidor:BasicHttpBinding con autenticación de certificado: ¿error "prohibido"?

var soapBinding = new BasicHttpBinding() { Namespace = "http://test.com" }; 
soapBinding.Security.Mode = BasicHttpSecurityMode.Transport; 
soapBinding.Security.Transport.ClientCredentialType = 
    HttpClientCredentialType.Certificate; 
var sh = new ServiceHost(typeof(Service1), uri); 
sh.AddServiceEndpoint(typeof(IService1), soapBinding, ""); 
sh.Credentials.ServiceCertificate.SetCertificate(
    StoreLocation.LocalMachine, StoreName.My, 
    X509FindType.FindBySubjectName, "localhost"); 
sh.Open(); 

aquí es el cliente:

var binding = new BasicHttpBinding(); 
binding.Security.Mode = BasicHttpSecurityMode.Transport; 
var service = new ServiceReference2.Service1Client(binding, 
    new EndpointAddress("https://localhost:801/Service1")); 

service.ClientCredentials.ClientCertificate.SetCertificate(
    StoreLocation.LocalMachine, StoreName.My, 
    X509FindType.FindBySubjectName, "localhost"); 

service.ClientCredentials.ServiceCertificate.Authentication. 
    CertificateValidationMode = 
     System.ServiceModel.Security.X509CertificateValidationMode.PeerTrust; 

service.HelloWorld(); 

Certificado para localhost está en Personal, raíz de confianza y contenedores tercera parte de confianza. Internet Explorer puede conectarse al host y ver WSDL. Además, las llamadas SSL funcionan bien con ClientCredentialType = HttpClientCredentialType.None

HelloWorld() falla con:

System.ServiceModel.Security.MessageSecurityException occurred<br/> 
    Message="The HTTP request was forbidden with client authentication 
    scheme 'Anonymous'." 

que es una excepción relanza de: "El servidor remoto devolvió un error: (403) Prohibida."

¿cómo se puede saber que está sucediendo wtf?

Respuesta

9

Trate de añadir esto en el cliente solo después de establecer Security.Mode:

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; 
+0

No puedo creer que era tan fácil y yo no cogerlo :( – galets

+0

Tengo el mismo problema, pero en el que el app.config tiene: en el elemento de transporte :-( – Ronnie

+0

Estoy tratando de resolver un problema similar - Acabo de probar su código, pero "agregar referencia de servicio" no funcionó en el cliente . Supongo que no podría adjuntar toda su solución? – user1229458

0

La respuesta ya está hecho, pero para otros:

si utiliza standart proxy generado que se configura en el app.config usted tiene que fijar transport ClientCredentialType a Certificate

(asegúrese de que el elemento XML no es <message clientCredentialType ... />)

  <binding name="SpoDataServiceSoap"> 
       <security mode="Transport"> 
        <transport clientCredentialType="Certificate"></transport> 
       </security> 
      </binding> 

C#

MyServiceSoapClient client = new MyServiceSoapClient() 
X509Certificate2 cert = CertificateHelper.GetClientCertificate(); 
client.ClientCredentials.ClientCertificate.Certificate = cert; 
Cuestiones relacionadas