Over a million developers have joined DZone.

Enabling SSL for a WCF Service

DZone's Guide to

Enabling SSL for a WCF Service

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Last week a reader mailed me with some questions about my “WCF over HTTPS” blog post, which I wrote almost 3 years ago.

I created some sample code to help him enable SSL for a WCF service. Last year this was my most popular article, so I thought it would make sense to create a new up-to-date version that shows you step-by-step how to enable SSL for a WCF service with as little fuss as possible.

Let’s get started…

Step 1 – The Service

First we are going to create a simple and easy-to-use WCF service. Start up Visual Studio 2010 and create a new blank solution called “SslEnabledWcfService”. Next add a new class library project to it called “CustomerService”.

Solution Explorer

Add a reference to the System.ServiceModel and System.Runtime.Serialization assemblies to the CustomerService project.

Add a new interface called ICustomerService to the project
that defines the service contract.

public interface ICustomerService
    IEnumerable<Customer> GetCustomers();

The service returns a collection of customers. Each customer is represented by an instance of the Customer class.

public class Customer
    public Guid Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

Go ahead and add a file called Customer.cs. Copy and paste the code listed above.

The actual service implementation is very simple. It returns a list of customers which I create on the fly. No use in dealing with a database, or another persistant data store for this post. Let’s keep things as simple as possible.

public class CustomerService : ICustomerService
    private IEnumerable<Customer> LoadCustomers()

    public IEnumerable<Customer> GetCustomers()
        var customers = new List<Customer>();
        return customers;

Check out the source code accompanying this post for the full code of the CustomerService class.


Step 2 – Configuration

OK, now it’s time to add some configuration for our service. The host process (IIS) needs this to be able to figure out how to host the service. First add a new application configuration file called Web.config to the CustomerService project.

Solution Explorer

First you need to list the service in the <system.serviceModel> node.

    <service name="CustomerService.CustomerService"

      <endpoint address=""
                contract="CustomerService.ICustomerService" />

      <endpoint address="mex"
                contract="IMetadataExchange" />


As you can see the service is linked to a custom service behavior called “MyServiceBehavior”. You need to define this behavior in the <behaviors> node of the <system.serviceModel> node.

    <behavior name="MyServiceBehavior">
      <serviceMetadata httpsGetEnabled="true" httpsGetUrl="" />
      <serviceDebug includeExceptionDetailInFaults="false"/>

Note that the httpsGetEnabled property of the behavior is set to true. This allows us to retrieve metadata for the service using an HTTPS/GET request. Handy for creating our client proxies later on.

The first endpoint (non-mex) of the service is also tied to a custom binding (basicHttpBinding) called TransportSecurity.

      <binding name="TransportSecurity">
        <security mode="Transport">
          <transport clientCredentialType="None" />

It is on the binding level that you must specify which security model the service uses. Here we set the mode to Transport (SSL) and turn off any type of client authentication.


Step 3 – Hosting The Service

The service has been created and configured. Time to host it. I’m using IIS 7.5 (7.5.7600.16385) on Windows 7 for this purpose. Create a new text file called “Service.svc”. Note the .svc extension!

Add it to the CustomerService project.

Solution Explorer

It only contains one line, namely:

<%@ ServiceHost Language="C#"
                CodeBehind="CustomerService.cs" %>

Here you identify the service, the language used and the location of the code behind file.

Open up Windows Explorer and navigate to the default installation folder for IIS (C:\inetpub). I started with a clean installation to make things easy. Since I don’t host any other sites locally I deleted everything I found within the wwwroot subfolder. You might want to create a subfolder within the wwwroot folder to host your service. Once you have done so create a new directory called bin within the wwwroot folder or your custom subfolder.

To host the service you must copy the following files to the directory in which you host your service:

  • Service.svc
  • Web.config
  • CustomerService.dll (\bin)

You must copy the compiled CustomerService.dll assembly (and any other dependent assemblies) inside the bin folder! Just set your solution configuration to Release, rebuild your solution and copy the necessary files as described.

Inetpub wwwroot

Almost there, we just need to configure IIS. Start up the Internet Information Services (IIS) Manager and navigate to the Default Web Site node in the left pane.

IIS Manager

Double click on the Default Document displayed in the middle pane under the IIS group.

Default Document

Remove all the documents listed there and afterwards add a new document called Service.svc. When we navigate to http://localhost we want IIS to serve up the Service.svc document by default.

Default Document

Open your favorite browser (*cough* Chrome * cough*) and navigate to http://localhost. You’ll be greeted by the following error page:

Secured Page

The page is secured and cannot be accessed via http. Exactly what we want! You can try and access it via https://localhost, but this will result in a page not found error as we have not yet configured SSL.


Step 4 – SSL Certificate

Before you can configure your service to use SSL you need a valid SSL certificate. Luckily you can create one yourself for development / testing purposes instead of purchasing one. Using the makecert.exe command-line utility you can create your own certificates.

Let’s quickly create a certificate. Start up an elevated Visual Studio Command Prompt and enter the following command:

makecert -r -pe -n “CN=YourComputerName” -b 01/01/2012 -e 01/01/2020
-eku -ss my -sr localMachine -sky exchange -sp
“Microsoft RSA SChannel Cryptographic Provider” -sy 12

Make sure to replace the term “YourComputerName” with your actual computer’s name. After executing the command you should get a simple “succeeded” message.



Step 5 – Enable SSL

Alright, we have our SSL certificate. Let’s bind it to our service. Go back to IIS Manager and select the Default Web Site node. In the right pane click on the “Bindings…” link.


In the Site Bindings popup click on the Add button to add a new binding.

Site Bindings

and enter the following data to enable SSL for your site:

Site Binding

Be sure to select the SSL certificate you created earlier! Just click OK to add the binding and close the Site Bindings popup afterwards.

Restart your web site and navigate to https://localhost. This time it’ll work, but you’ll probably get a warning message because of your untrusted SSL certificate. Just ignore it.

SSL Enabled Service


Step 6 – Consume The Service

Now that we have our service running in IIS and secured with SSL let’s test it. Time to consume the service. Add a new console application to the solution called ClientApp. Add a service reference to our newly created WCF service.

Add Service Reference

When adding the service reference Visual Studio will report a problem with the SSL certificate.

Certificate Problem

Just click Yes to proceed. The message is shown because the certificate has not been issued by a company you have chosen to trust.

After you’ve created the service reference you can consume the service. Let’s display a list of the customers.

using (var proxy = new CustomerServiceClient())
    var customers = proxy.GetCustomers();
    foreach(var customer in customers)
        Console.WriteLine(String.Format("{0} {1}", customer.FirstName, customer.LastName));

When you try to execute this code you’ll get the following exception:


The client application does not trust the service. You can fix this by inspecting the certificate which the service hands over to the client. You need to hook up a handler for the ServicePointManager’s ServerCertifcateValidationCallback.

ServicePointManager.ServerCertificateValidationCallback += customXertificateValidation;

When this callback is triggered you can inspect the server certificate.

private static bool customXertificateValidation(object sender, X509Certificate cert,
    X509Chain chain, SslPolicyErrors error)
    var certificate = (X509Certificate2) cert;

    // Inspect the server certficiate here to validate 
    // that you are dealing with the correct server.
    // If so return true, if not return false.
    return true;

We’re finally there. If you run the client application now it will correctly list the customers returned by the SSL-enabled WCF service.

The Customers

You can download the source code accompanying this article from the download page. If you have any questions or suggestions please drop me an e-mail or submit a comment.


Source: http://cgeers.com/2012/01/30/enabling-ssl-for-a-wcf-service/


Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

dotnet ,ssl ,wcf ,.net & windows

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}