.net – Client cannot find the X.509 certificate

netwcfx509certificate

I'm testing a WCF service on a local workstation, and having difficulties with the X509 certificate.

I've created a certificate and registered it successfully. The certificate shows in Certificate Manager under Trusted People/Certificates. The service behaviour is configured as follows (names commented as *):

<serviceCertificate findValue="*****" storeLocation="LocalMachine"
              storeName="TrustedPeople" x509FindType="FindBySubjectName" />
<userNameAuthentication userNamePasswordValidationMode="Custom"
              customUserNamePasswordValidatorType="*****.DistributorValidator, *****" />
<issuedTokenAuthentication certificateValidationMode="None" />

But, when the service is opened in browser, I get the error "System.InvalidOperationException: Cannot find the X.509 certificate using the following search criteria: StoreName 'TrustedPeople', StoreLocation 'LocalMachine', FindType 'FindBySubjectName', FindValue "*‎‎***".

The same error happens when I try to set certificate programmatically in test client:

 serviceclient.ClientCredentials.ClientCertificate.SetCertificate(
                        StoreLocation.LocalMachine,
                        StoreName.TrustedPeople,
                        X509FindType.FindBySubjectName,
                        "CN=TravelBrokerKey");

It seems that finding by subject name, thumbprint or serial number makes no difference. I also tried to catch the exception and debug, but could not attach the debugger to it (so it seems that the exception happens in client side). Also tried to register the certificate directly through browser settings, but this did not help either.

The test environment OS is Windows 7 and tested with browsers IE8 and Firefox 4.0.

Best Answer

I think you make wrong use of the certificate, the certificate in your case is used to identify the service, and I think your service requires client side use Username client credential type since I see you provide a custom UserNamePasswordValidator, so you should set username and password in your ClientCredentials.

You should specify the service's certificate in your client side but not in ClientCredentials.ClientCertificate, which is used for client's certificate.

Following links may help you:

An easy way to use certificates for WCF security

How to: Use Certificate Authentication and Message Security in WCF Calling from Windows Forms

Related Topic