Click here to Skip to main content
Licence CPOL
First Posted 3 May 2007
Views 106,113
Downloads 1,006
Bookmarked 34 times

WCF Transport Layer Security using wsHttpBinding and SSL

By | 3 May 2007 | Article
WCF Webservice on IIS6.0 using SSL

Introduction

This article is useful for developers who are interested in implementing WCF webservice using transport layer security and SSL configured on IIS6.0. Those who do not have a good idea about WCF can read more about it here and here.

Using the code

You can go through the web.config file in the project folders which I have uploaded.

<system.serviceModel>
 <services>
   <service behaviorConfiguration="returnFaults" name="TestService.Service">
      <endpoint binding="wsHttpBinding" bindingConfiguration=
            "TransportSecurity" contract="TestService.IService"/>
      <endpoint address="mex" binding="mexHttpsBinding" 
            name="MetadataBinding" contract="IMetadataExchange"/>
  </service>
 </services>
 <behaviors>
   <serviceBehaviors>
    <behavior name="returnFaults">
     <serviceDebug includeExceptionDetailInFaults="true"/>
       <serviceMetadata httpsGetEnabled="true"/>
       <serviceTimeouts/>
   </behavior>
  </serviceBehaviors>
 </behaviors>
 <bindings>
    <wsHttpBinding>
       <binding name="TransportSecurity">
             <security mode="Transport">
              <transport clientCredentialType="None"/>
              </security>
        </binding>
      </wsHttpBinding>
 </bindings>
 <diagnostics>
  <messageLogging logEntireMessage="true" 
    maxMessagesToLog="300" logMessagesAtServiceLevel="true" 
    logMalformedMessages="true" logMessagesAtTransportLevel="true"/>
  </diagnostics>
 </system.serviceModel>

//Contract Description
[ServiceContract]
interface IService
{
  [OperationContract]
   string TestCall();
}

//Implementation
public class Service:IService
{
  public string TestCall()
  {
      return "You just called a WCF webservice On SSL
                    (Transport Layer Security)";
  }
}

//Tracing and message logging
<system.diagnostics>
  <sources>
      <source name="System.ServiceModel" 
    switchValue="Information,ActivityTracing" propagateActivity="true">
         <listeners>
           <add name="xml"/>
        </listeners>
      </source>
        <source name="System.ServiceModel.MessageLogging">
        <listeners>
            <add name="xml"/>
         </listeners>
         </source>
    </sources>
        <sharedListeners>
          <add initializeData="C:\Service.svclog" 
        type="System.Diagnostics.XmlWriterTraceListener" name="xml"/>
         </sharedListeners>
       <trace autoflush="true"/>
</system.diagnostics>

In the above ServiceModel configuration, there are two end points:

  1. One with contract TestService.IService: In this, binding is configured to have transport layer security , see inside the <bindings> tag. So SSL has to be configured on IIS.

  2. One with contract IMetadataExchange: this is also configured to an HTTPS call. If you see the binding it is mexHttpsBinding, and in the service behaviors section, httpsGetEnabled is used, here I tried to even secure the metadata publishing through WSDL.

To configure this Web.config file you can use SvcConfigEditor.exe which is located in
C:\program files\microsoft sdks\windows\v6.0\bin\svcconfigeditor.exe

If you try to run the code from Visual Studio then you get an exception as shown below:
"Could not find a base address that matches scheme HTTPS for the endpoint with binding WSHttpBinding. Registered base address schemes are [HTTP]."

So first configure the website on SSL. To get an idea on how to configure SSL, you can go through this. Make sure that when you configure the SSL, the certificate CN value should be exactly the same as the URL of the website. For example, if your webservice address is http:\\www.example.com, then issue a certificate on the name : CN = http:\\www.example.com.

Don't forget to host an entry in the hosts file c:\windows\system32\drivers\etc\hosts. If you want to put this on localhost then just enter the following in the host file 127.0.0.1 www.example.com.

Configure www.example.com as the header value in the website properties on port 80. Once you are done with SSL, you will access the webservice through the web browser as https://www.example.com/service.svc. On this page you will have the HTTPS URL for WSDL .

I have even enabled tracing and message logging on the webservice. To view the service log just use svctraceviewer.exe by loading service.log file in this. See the <system.diagnostics> tag above

Note that I have not put any certificates to run this sample. So if you want to run this sample, then generate a certificate, install it on IIS as per the instructions above and run it though the browser. To get an idea how to generate self certificates for testing purposes just go through this link.

To run this project you need to have IIS 6.0 on your machine. On IIS 5.0 also you can do that, but it needs to be configured to run WCF services.

Hope this article helps you get a good idea about WCF transport layer security and SSL. If you have any question or comments please email me, I would really appreciate it. Thanks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohan Pindyala

Software Developer (Senior)
Rovi Corporation
United States United States

Member

Working on web services , Web services security , SAML2.0,WSE, WCF and Asp.net Ajax Applications .

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberMember 820491521:11 2 Nov '11  
GeneralMy vote of 1 Pinmemberan_phu23:30 4 Aug '10  
Generalmore detail please PinmemberSteve Cav19:18 18 May '10  
AnswerService does not respond PinmemberBenjaminRumpf4:22 14 Dec '09  
Questionwhat about duplex channel? Pinmemberslava_pvf0:17 27 May '08  
AnswerRe: what about duplex channel? PinmemberMohan Pindyala16:34 27 May '08  
You have to figure it out , it has been long time since I worked on that stuff, currently busy in dynamics CRM. If I get time , I will look into it
thanks
Mohan
 
Beyond Imagination

GeneralHttps Exception PinmemberMiss C13:44 24 Sep '07  
GeneralRe: Https Exception PinmemberMohan Pindyala19:11 24 Sep '07  
GeneralAwesome article! Pinmemberzdex8:26 15 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120604.1 | Last Updated 4 May 2007
Article Copyright 2007 by Mohan Pindyala
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid