Welcome to MSDN Blogs Sign in | Join | Help

WCF – 2 Way SSL Security using Certificates

I had to work extensively in this topic, and had to go through hundreds of blogs and articles to finally make it work. It’s actually pretty simple but for someone who is new to WCF, it might give a few sleepless nights and some terrible days.

This article assumes that you have a decent knowledge on WCF, IIS hosted WCF Services, transport security and digital certificates.

Configuring the Service:

Open your Service’s web.config file and edit it’s binding configuration as below:

 <bindings>
  <wsHttpBinding>
    <binding name="CertificateWithTransport">
      <security mode="Transport">
        <transport clientCredentialType="Certificate" />
      </security>
    </binding>
    </wsHttpBinding>
</bindings>

The above binding configuration uses wsHttpBinding with Transport mode security. You can also notice that the clientCredentialType is mentioned as “Certificate”. It means, that the consumer of the service is authenticated using a certificate.

Your behavior section should look like this,

<serviceBehaviors>
       <behavior name="BindingBehavior">
                  <serviceMetadata httpsGetEnabled="true"/>
                  <serviceDebug includeExceptionDetailInFaults="true"/>

       </behavior>
     </serviceBehaviors>

Notice that the httpsGetEnabled=”true” attribute. It tells WCF to serve get request on the service. This is should be set to true when you want the consumers to get the wsdl themselves.

If this is set to false, no one will know about this service, unless you give them the wsdl file for consumption. Most of the enterprise services do set this attribute to false for security reasons.

Your Services section should look like this,

<services>
      <service behaviorConfiguration="BindingBehavior"
        name="servicename">
        <endpoint binding="wsHttpBinding"         contract="SSPNotificationReceiver.IReceiverService" bindingConfiguration="CertificateWithTransport">
        </endpoint>
        <!--<endpoint address="mex" binding="mexHttpsBinding"
            name="MetadataBinding" contract="IMetadataExchange"/>-->
      </service>
</services>

The above behavior configuration basically exposes an endpoint with information about the binding type, service contract type and the binding configuration. The bindingConfiguration attribute is the link between the binding settings and the endpoint, and the behaviorConfiguration attribute is the link between the behavior settings and the service settings.

The commented endpoint is for the meta data exchange. Since we are using SSL, the meta data exchange is also secure. But beware, may be this is the most important statement in the entire article. There is something very important about commenting this part at the end of the article.

Setting up IIS and creating the Certificates:

Now, publish the service on IIS using Visual Studio. The following screen shots show you how to configure the IIS SSL mappings.

The following example will show you how to create SSL Server and SSL client certificates using a Certificate Authority running on Windows Server 2003. You can also create your own certs for development through makecert.exe. There are multiple articles on the internet showing how to create certificates using the tool. Just do a Live Search and find out.

  • Set Anonymous Access on IIS for your website (another important task which you might easily miss out) and remove all other authentication modes.

image

  • On the Directory Security tab, click Server Certificate and choose “Create new Certificate”. Follow the wizard to generate the certificate request. The certificate request is by default created in “c:\certreq.txt”
  • Open the Certtificate Services Website. It is generally http://localhost/certsrv on the server which is running the certificate authority.

image

  • Click on the Request a Certificate link

image

  • On the next screen, click on advanced certificate request

image

  • Choose the second option in the above screen

image 

  • In the next screen, paste the contents of the c:\certreq.txt file in the saved request textbox as shown above.
  • After you click submit, the certificate is created and queued in the Pending Requests on the Certificate Authority.
  • Open Certificate Authority in the Windows Server 2003 machine

image

  • Issue the certificate.
  • Again, go back to the Certification Services and click on the “View Status of the pending requests” link in the home page.

image

  • Click on Download Certificate link and save the certificate to a location and name it SSLServerCert.cer

 image

With this, you have successfully created the SSL Server Certificate which can be used for Server Authentication purpose.

Follow these steps to create a Client Authentication certificate.

  • On the Certificate Services home page, click on Request a Certificate option which will take you to the page shown below.

image

  • Click on advanced certificate request option in this page.
  • In the next page, click on “Create and submit a request to CA” option
  • Choose “Client Authentication Certificate” in the Type of certificate needed drop down.
  • Give all text boxes with appropriate information in the Identifying Information section.
  • Check “Mark Keys as exportable” option

image

  • Click Submit
  • As you did for SSL Server Certificate, go to Certification Authority and Issue this certificate as well
  • Come back to the Certificate Services again and click on Install Certificate link

image

  • Your Certificate will be installed in your Personal Store as shown below

image

  • You can export this cert with private key and send it across to the client who is going to use the certificate for authenticating itself.

Configuring IIS for 2 Way SSL Authentication

  • On your website’s directory security tab, click on Server Certificate
  • Click next and choose the option as shown below

image

  • Follow the wizard and select the SSLServerCert.cer that you already saved
  • Once finished, again click Edit button in the Secure Communications Section in Directory Security tab.

image

  • Check the Require secure channel (SSL) option
  • Choose Require client certificates under Client Certificates section

Do not do the following steps if your service needs only SSL Server Authentication. To enable SSL Client authentication also, proceed with the following steps

  • Check Enable client certificate mapping option
  • Click Edit to create a 1-1 certificate mapping
  • Browse to the client certificate that you already created and give map a local user account to it. All requests that carry this certificate will run using the account that you give here
  • Now, you have completed the entire hosting part of the service. The Service that you created is secured by SSL Server as well as SSL Client authentication.

Configuring the client to use SSL

<system.serviceModel>
      <bindings>
        <wsHttpBinding>
          <binding name="WSHttpBinding_IReceiverService">
            <security mode="Transport">
              <transport clientCredentialType="Certificate" />
            </security>
          </binding>
        </wsHttpBinding>
      </bindings>
        <client>
            <endpoint address=
https://YourIP/ReceiverHost.svc behaviorConfiguration="credentialConfiguration"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IReceiverService"
                contract="NotificationProxy.IReceiverService" name="WSHttpBinding_IReceiverService" />
        </client>
      <behaviors>
        <endpointBehaviors>
          <behavior name="credentialConfiguration">
            <clientCredentials>
              <clientCertificate findValue="99bbc6c9e6f4a6bd526bc8bb21f9c21f0716c23r"
          storeLocation="CurrentUser"
          x509FindType="FindByThumbprint" />

              </clientCredentials>
          </behavior>
        </endpointBehaviors>
      </behaviors>
    </system.serviceModel>

I have pretty much posted the entire client configuration here. There is nothing very different from the service configuration, but for the behavior and the endpoint address.

Now, you are all set to access the service through the client.

You can access the service using the browser too..

When you do so, the browser (IE) pops up a window showing all the certificates installed in your personal store. You need to select a the certificate that you already installed in it

image

Select the appropriate certificate to view the service screen.

image

That’s it. Your service is up and running on IIS, secured using 2 Way SSL Authentication.

Two most important things not to forget:

As I said earlier there are two things that you should do:

NEVER FORGET TO ENABLE ANONYMOUS ACCESS ON IIS.

NEVER FORGET TO COMMENT THE MEX ENDPOINT ON THE SERVICE CONFIG

These two things are the most crucial part of this entire process. WCF, for some reason that I don’t know, wants Anonymous Access to be enabled on the website. You will get this error if you don’t do so.

Exception: System.ServiceModel.ServiceActivationException: The service '/ReceiverHost.svc' cannot be activated due to an exception during compilation.  The exception message is: Security settings for this service require 'Anonymous' Authentication but it is not enabled for the IIS application that hosts this service.. --->

Next, if you forget to leave the mex endpoint uncommented, you might end up seeing this error on the event log of the service.

Exception: System.ServiceModel.ServiceActivationException: The service '/ReceiverHost.svc' cannot be activated due to an exception during compilation.  The exception message is: The SSL settings for the service 'None' does not match those of the IIS 'Ssl, SslNegotiateCert, SslRequireCert, SslMapCert'.. –->

As you can see, this does not convey any reasonable thing to you, leave alone suggestions to fix the error. Be careful with these two settings.

Enjoy WCF !!

Published Friday, September 12, 2008 3:59 PM by imayak

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: WCF – 2 Way SSL Security using Certificates

Wednesday, December 10, 2008 4:14 PM by Prashant

Hey I got nice help from blog.

but I am still endup with Exception.

"The SSL settings for the service 'None' does not match those of the IIS 'Ssl, SslNegotiateCert, SslRequireCert, SslMapCert'"

If i change trasport secutity from "trasport" to "trasportwithMessageCredential" It works. But again I end up with "Could not establish trust relationship for the SSL/TLS secure channel with authority"

Can you tell me the cause.

# re: WCF – 2 Way SSL Security using Certificates

Wednesday, December 10, 2008 9:41 PM by imayak

For this error "Could not establish trust relationship for the SSL/TLS secure channel with authority", check that the SSL Certificate that you have is trusted on the client.

That is, the SSL certificate issuer should be in the Trusted Store of your client computer. If your SSL certificate is issued from a Trusted CA like Verisign,you shouldn't be facing this problem because all of the known Verisign issuers are already trusted.

However, if you are testing with a temporary certificate which you created using makecert.exe or got from a test CA like Comodo, you will face this problem.

If you still want more clarifications, let me know what kind of binding configuration you are using, so that I can help you out with the right settings.

Good luck.

# re: WCF – 2 Way SSL Security using Certificates

Thursday, December 11, 2008 2:36 PM by Manpreet

Excellent description. Keep up the good work.

# re: WCF – 2 Way SSL Security using Certificates

Sunday, December 14, 2008 10:49 PM by Anony Mouse

Good blog. Thanks.

I am a little confused about anonymous access too. Can you write a wcf service that just uses anonymous access without encryption and without windwos authentication?

I know you would not want to do this, but want to know if you can.

Otherwise is it safe to assume all wcf services must use certificate based authentication?

# re: WCF – 2 Way SSL Security using Certificates

Monday, December 15, 2008 12:15 AM by imayak

You can always set the WCF service to use no authentication at all. Of course no one would want to do that, but its possible.

On your second question, it is not required for all WCF services to use certificate based authentication.

WCF Services must be configured for SSL client certificate authentication only if the clients are already "known" to the service. (ie., Intranet applications or when you have a defined set of customers to access the service)

Internet based non-critical services generally don't use client certificates.

# re: WCF – 2 Way SSL Security using Certificates

Wednesday, January 07, 2009 4:50 PM by John

# re: WCF – 2 Way SSL Security using Certificates

Wednesday, January 07, 2009 6:41 PM by John

You can leave the mex endpoint uncommented if you configure it to use wsHttpBinding and the same bindingConfiguration as your service uses.  For example,

<endpoint address="mex" binding="wsHttpsBinding"

 name="MetadataBinding" contract="IMetadataExchange" bindingConfiguration="CertificateWithTransport"/>

For more info see:

http://msdn.microsoft.com/en-us/library/aa395212.aspx

(Custom Secure Metadata Endpoint)

# re: WCF – 2 Way SSL Security using Certificates

Monday, February 23, 2009 2:53 PM by Maxim

Nice and useful article!

But even after enabling anonymous in IIS and disabling mex, I still get errors:

Service error: The HTTP request was forbidden with client authentication 'Anonymous'. Service Fault: The remote server returned an error: (403) Forbidden.

What could be the reason?

# re: WCF – 2 Way SSL Security using Certificates

Monday, April 27, 2009 12:44 PM by Ashley Tate

@Maxim: I ran into the same error and have documented at least one cause here:

http://coditate.blogspot.com/2009/04/confusing-errors-using-wcf-transport.html

# re: WCF – 2 Way SSL Security using Certificates

Monday, April 27, 2009 11:39 PM by imayak

@Maxim : It's true that sometimes the error messages aren't very clear and helpful in troubleshooting. Just go through the settings once again and ensure that everything is right.

# re: WCF – 2 Way SSL Security using Certificates

Wednesday, May 13, 2009 4:50 AM by meir

great article.

but when i checked the Require SSL in iis (7.0).

then every time i get Internal Server Error.

why?

# re: WCF – 2 Way SSL Security using Certificates

Wednesday, May 13, 2009 5:09 AM by meir

i found out that if iis is configured as Require SSL, i had 2 comment the HTTP binding/endpoint swctions  in the web.config to get it to work.

why?

# re: WCF – 2 Way SSL Security using Certificates

Wednesday, May 20, 2009 9:06 PM by Chris Mullins

I was able to get the MEX endpoint to work using this same approach. The issues is the binding and bindingConfiguration of the endpoint.

Step 1 was to create a service behavior that required SSL.

   <behaviors>

     <serviceBehaviors>

       <behavior name="MyName">

         <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false"/>          

       </behavior>

     </serviceBehaviors>

    </behaviors>

Step 2 is to enable the MEX endpoint using the wsHttpBinding and proper binding configuration. In this case, the binding configuration is the same as the primary endpiong name.

<endpoint address="mex" binding="wsHttpBinding" contract="IMetadataExchange" bindingConfiguration="MyBinding" />

Doing this worked fine. To get to the MEX endpoint, a client cert is required - which is exactly what I wanted.

# re: WCF – 2 Way SSL Security using Certificates

Thursday, May 21, 2009 1:00 AM by imayak

Meir >> I can't get your problem. Can you post your bindings as well?

# re: WCF – 2 Way SSL Security using Certificates

Tuesday, June 09, 2009 12:06 PM by Jaster

Awsome Work!

But i'm still expirencing some trouble. I can create an instance of the service, but i can't call any methods. i recieve an errormessage like: "could not establish trusted ssl/tsl channel..."

any ideas?

Regards

Jaster

# re: WCF – 2 Way SSL Security using Certificates

Wednesday, June 10, 2009 5:02 AM by imayak

It's basically a certificate trust issue. The client machine that you are using to talk to the server does not trust the certificate that you are using on your server.

Make sure the certificate's 'CN' property has the domain name.

Eg., If the WCF Service URL is : https://myserver/services/service.svc

then, your certificate should be issued to "myserver". i.e., you should see CN=myserver in the certificate properties.

If this is also done, then install the certificate in the trusted store of the client machine. It should work.

# re: WCF – 2 Way SSL Security using Certificates

Sunday, June 21, 2009 9:03 AM by T.Chowdhury

Very Good Article, follwoing your steps I was able to install my service using SSL. Although few of my steps somehow differ than what it has stated in the article. I will comeback to later on that.

First I have few questions in code below (Configuring the client to use SSL) which certificate thumb print represents below Client or Server? My second question is how do I know what's the value of

storeLocation?

<behavior name="credentialConfiguration"><clientCredentials><clientCertificate findValue="99bbc6c9e6f4a6bd526bc8bb21f9c21f0716c23r" storeLocation="CurrentUser" x509FindType="FindByThumbprint" />

# re: WCF – 2 Way SSL Security using Certificates

Monday, June 22, 2009 1:19 AM by imayak

Chowdhury,

In the behavior, you must give the client certificate's thumbprint value.

In the storeLocation attribute, give the name of the store where the certificate resides. There are multiple certificate stores on the computer and here is how you open them.

On Run -> Type mmc and press Enter

Press Ctrl +M

On Add or Remove Snap-ins window, select Certificates and click Add button

A new window with

1. My User Account

2. Service Account

3. Computer Account

appears

If you select "My User Account", you will be able to see all certificates accessible to that particular windows user account.

If you select Service Account, you will be able to see all certificates accessible to that particular windows service that you will eventually choose in the next window.

Sample theory applies to the third option as well.

# re: WCF – 2 Way SSL Security using Certificates

Tuesday, July 28, 2009 11:44 PM by Elena Neroslavskaya

Thank you for the great post. The only problem we have noticed that once Anonymous is enabled on IIS, ANY certificate issued by CA that is in trusted authorities store is granted access.

Is there a possibility to restrict access to just one specific Cert?

# re: WCF – 2 Way SSL Security using Certificates

Saturday, August 01, 2009 11:09 PM by T.Chowdhury

Hi, here are some good news and bad news. Good news is following your steps, I was able to install my service using SSL as well as consume the service from a client (console application). No issue.

The app config file used for the client console application shown as below

<system.serviceModel>

       <bindings>

           <wsHttpBinding>

               <binding name="EmailServiceHttpsEndpoint">

                   <security mode="Transport">

                       <transport clientCredentialType="Certificate" />

                   </security>

               </binding>

           </wsHttpBinding>

       </bindings>

       <client>

           <endpoint address="https://tnsiit.tnsinsideit.local/EmailService/EmailService.svc"

               behaviorConfiguration="credentialConfiguration" binding="wsHttpBinding"

               bindingConfiguration="EmailServiceHttpsEndpoint" contract="EmailClient.EmailService.IEmailService"

               name="EmailServiceHttpsEndpoint" />

       </client>

     <behaviors>

       <endpointBehaviors>

         <behavior name="credentialConfiguration">

           <clientCredentials>

             <clientCertificate findValue="c72e3caa4ae63e20791f10bc6ab99ef24a2cc34d"   storeLocation="CurrentUser"  x509FindType="FindByThumbprint" />

           </clientCredentials>

         </behavior>

       </endpointBehaviors>

     </behaviors>

   </system.serviceModel>

The bad news is after that I have created an ASP.net application, trying to consume the same service. I have cut & paste the above configuration into ASP.net web config, we I was testing the above service via aspx page, I'm getting this error

Cannot find the X.509 certificate using the following search criteria: StoreName 'My', StoreLocation 'CurrentUser', FindType 'FindByThumbprint', FindValue 'c72e3caa4ae63e20791f10bc6ab99ef24a2cc34d'.

Do I have to do something different for ASP .NEt  application.

Any help will be really appreciable.

# re: WCF – 2 Way SSL Security using Certificates

Thursday, August 06, 2009 12:34 AM by erosen03

Thanks for a great post. I had a similar issue trying to figure out why a particular WCF service kept reporting that it needed anonymous authentication enabled in IIS, even after I corrected the items in the WCF service's web.config file. It turned out for me that even though web.config changes are supposed to be applied immediately after the file is saved, I had to issue an IISRESET command in order for the error message about anonymous access to go away.

# re: WCF – 2 Way SSL Security using Certificates

Tuesday, August 11, 2009 10:37 PM by Sahil Malik

Hi Imaya,

This is a good article, and you being the original source, it has been shamelessly copied all over the internet by people claiming it as their own. Anyway, credit goes to you for the original.

I wanted to point out a couple of things though.

a) Setting anonymous is not necessary. You've had to set anonymous over there because your service settings are not properly translating the certificate identity into an identity that the service will understand, i.e. an AD identity for instance. Try doing the mapping using serviceBehavior/service-clientCredentials .. and you'll see what I'm saying.

b) Metadata exchange does not need to be turned off either. You simply need to do httpsEnabled - true, or enable metadata over HTTPS as one other comment mentioned above.

Great post otherwise. Love it.

Sahil

# re: WCF – 2 Way SSL Security using Certificates

Wednesday, August 12, 2009 12:27 AM by imayak

Thanks Sahil.

I appreciate your clarification for the issues mentioned.

# re: WCF – 2 Way SSL Security using Certificates

Wednesday, August 12, 2009 5:13 PM by Manav

Hi Imaya,

Thanks for the great post. I exactly had the same issues, and your article pulled me out. My only grievence is I found you article when I had struggled thru and fixed almost all the issues, and was stuck at the last one - related to commenting the mex endpoint.

Still a great post. Keep up the good work.

Manav

# re: WCF – 2 Way SSL Security using Certificates

Sunday, September 06, 2009 6:44 AM by Alex

Hi Imaya,thanks for your great post.

By the way,what do you mean by terms of "2 way SSL security?"

what is the typical usage scenario for it?

# re: WCF – 2 Way SSL Security using Certificates

Monday, September 07, 2009 2:00 AM by imayak

@Alex : It's just that the client will also have a SSL certificate so the server can authenticate the client. So, I call it '2 Way'.

# re: WCF – 2 Way SSL Security using Certificates

Monday, October 19, 2009 5:26 PM by Chloé

hi imayak,

thank you for taking your time and help us all. i have a problem =)

before i found your post, i already figured out how to set up https on my dev machine running xp as well as my QA 2003 server with self certificate.

my project (website) is a distribution website, therefore, i cannot use static url in the web.config or in the clientconfig. i find the Uri and create the proxy from there.

everything works great! with two end points in the web.config file.

1 - basicHttpBinding

2 - wsHttpBinding

hitting the wsdl with http or https works, however, when i set SSL Required on, i can't get to the wsdl at all. if i remove basicHttpBinding section or wsHttpBinding and remove httpGetEnabled (basicHttpBinding) or httpsGetEnabled (wsHttpBinding) out of the serviceMetadata, then it works.

Question 1) Any thought?

Question 2) do i need those end points in the web.config? Can i do it dynamically when i about to call WCF?

I'm new to WCF as well, so if you or anyone knows how to create a dynamic end point w/out any end point in the web.config, please feel free to give me info.

thank you very much.

Chloé

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker