2012-01-10 17 views
10

Come si configura un client di servizio utilizzando l'autenticazione di certificato a livello di programmazione in C#?
E non voglio usare .config.Configurare il client di servizio WCF con autenticazione certificato a livello di programmazione

 using(var srv = GetServiceInstance()) 
     { 
      srv.DoStuff() 
     } 

     private TheServiceClient GetServiceInstance() 
     { 
      var service = new TheServiceClient(CreateWsHttpBinding(), CreateEndpointAdress()); 
      return service; 
     } 
     private static WSHttpBinding CreateWsHttpBinding() 
     { 
     var wsHttpBinding = new WSHttpBinding(); 
     wsHttpBinding.Security.Mode = SecurityMode.Message; 

     wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; 
     wsHttpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None; 
     wsHttpBinding.Security.Transport.Realm = ""; 
     wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; 

     wsHttpBinding.Security.Message.NegotiateServiceCredential = true; 
     wsHttpBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; 

     wsHttpBinding.Name = "Bindingname"; 
     wsHttpBinding.CloseTimeout = TimeSpan.FromMinutes(1); 
     wsHttpBinding.OpenTimeout = TimeSpan.FromMinutes(1); 
     wsHttpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10); 
     wsHttpBinding.SendTimeout = TimeSpan.FromMinutes(1); 
     wsHttpBinding.BypassProxyOnLocal = false; 
     wsHttpBinding.TransactionFlow = false; 
     wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; 
     wsHttpBinding.MaxBufferPoolSize = 524288; 
     wsHttpBinding.MaxReceivedMessageSize = 65536; 
     wsHttpBinding.MessageEncoding = WSMessageEncoding.Text; 
     wsHttpBinding.TextEncoding = Encoding.UTF8; 
     wsHttpBinding.UseDefaultWebProxy = true; 
     wsHttpBinding.AllowCookies = false; 

     wsHttpBinding.ReaderQuotas.MaxDepth = 32; 
     wsHttpBinding.ReaderQuotas.MaxArrayLength = 16384; 
     wsHttpBinding.ReaderQuotas.MaxStringContentLength = 8192; 
     wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 4096; 
     wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 16384; 

     wsHttpBinding.ReliableSession.Ordered = true; 
     wsHttpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10); 
     wsHttpBinding.ReliableSession.Enabled = false; 

     return wsHttpBinding; 
    } 
     private static EndpointAddress CreateEndpointAdress() 
     { 
      var store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser); 
      store.Open(OpenFlags.ReadOnly); 
      var cert = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, "CN=Certname", false)[0]; 
      store.Close(); 

     var endpointIdentity = EndpointIdentity.CreateX509CertificateIdentity(cert); 
     var endpoint = new EndpointAddress(new Uri("ServiceUri"), endpointIdentity); 

     return endpoint; 
    } 

Quindi questo è quello che ho finora! L'utilizzo restituisce un errore dicendo:

Il certificato client non è fornito. Specificare un certificato client in ClientCredentials.

Qualcuno ha un'idea? Sii gentile, sono nuovo di questo!

risposta

9

Come rilevato nei commenti sull'altra risposta, è necessario impostare service.ClientCredentials.ClientCertificate.Certificate direttamente.

0

potrebbe essere necessario per vedere se tale certificato è visibile a l'account del computer - Supponendo che hai debug al punto seguente:

var cert = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, "CN=Certname", false)[0];

ed è dicendo che non riesce a trovarlo?

+0

il cert viene trovato e impostato. service.ClientCredentials.ClientCertificate.Certificate è null – espvar

+0

Non si intende impostare direttamente 'service.ClientCredentials.ClientCertificate.Certificate'? È documentato come read-write e per altre forme di autenticazione client, ho impostato ad es. 'ClientCredentials.UserName.UserName' e' ClientCredentials.UserName.Password' direttamente prima. Penso che la roba 'EndpointIdentity' sia per endpoint di servizio, non client. – shambulator

+0

Questo è quello che ho capito anche. Quando metto 'service.ClientCredentials.ClientCertificate.Certificate = store.Certificates.Find (X509FindType.FindBySubjectDistinguishedName," CN = CertName ", false) [0];' Nel metodo GetServiceInstance ha funzionato. Avevo anche bisogno di mantenere EndpointIdentity dove ho aggiunto 'var endpointIdentity = EndpointIdentity.CreateDnsIdentity (" DnsIdentity ");' – espvar

0

Penso che manchi la descrizione del contratto. Ecco, c'è un piccolo esempio su come farlo. Se hai ulteriori problemi, ho un codice operativo completo. Ti aiuterò.

Contratto ContractDescription = ContractDescription.GetContract (typeof (IEvalService), typeof (EvalServiceClient));

È necessario inizializzare con il client prima di aprire il proxy. Spero che funzionerà.

divertirsi

Problemi correlati