IIS Express - Host WCF service over SSL
In my previous article, I described about configuring virtual directory under IIS Express. Here I am going to explore about setting up a WCF service using iis express and setting the transport security over SSL. We'll go step by step.
Setup SSL
If you have experience with setting up SSL on IIS, it is very similar on iis express, except you don't have an GUI tool support, you will need to use proper utilities to do it manually. In a high level, we will need to perform following tasks to achieve this.
- Get an appropriate certificate.
- Create an HTTPS binding on the site.
- Create a secure WCF service.
- Test the changes.
Steps:
- First, run the command - c:\> netsh http show sslcert - list out all the existing certificate on the machine. You will see the out put as figure below.
- Now, we'll re-use one of these certificate on a new port 442. In my case, I am going to re-use the certificate on port 44300, which is installed by the WebMatrix tool. In order to re-use, we need to delete this entry from the existing location. Use following command to do that.
c:\> netsh http delete sslcert ipport=0.0.0.0:44300 - Now we can add this certificate on port 442 with the command
netsh http add sslcert ipport=0.0.0.0:442 certhash=<certhash> appid=<appid>
Use the same certificate hash and appID from the above list.
Create an HTTPS binding on the site
This can be done in two ways.
- Use appcmd.exe (found under the iis express installation folder) and run it with following parameters as
c:\> appCmd.exe set config "Default Web Site" -commitPath:APPHOST -section:access -sslFlags:Ssl - Or you can manually edit the applicationhost.config file and add following entry in it.
<system.webServer>
<security>
<access flags="Script, Read" sslFlags="Ssl" />
</security>
</system.webServer>
Edit site node as follows:
<site name="WebSite1" id="1" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="C:\Hari\IISExpress\WebSite1" />
</application>
<bindings>
<binding protocol="https" bindingInformation=":442:localhost" />
</bindings>
</site>
Now, if you browse your test page under website1 with https in the url, you should see the test page - https://localhost:442/Webform1.aspx. The security warning that you see first is because you are using a self-signed certificate.
Create a secure WCF service
I am not going in too detailed about this just to stay focused on the topic.; may be I will explain in high level and can provide the source code which will give enough details.
When you host WCF under ssl, you are enabling the security mode as "Transport". In a high level, you can follow these steps to create a secure service.
- Create basic service with wsHTTPBinding.
- Add / change endpoint to use the new https://... address. Also specify a new bindingConfiguration to enable transport security.
- Add new binding with security mode="Transport"
Once you enable the WCF service to run with https protocol, you can test the same in browser by navigating to the given address. Now, deploy this new WCF service under the same website1 configured under iis express. Remember, you will need to have a test page also to call the service and to confirm whether your service is working as expected.
Note: You may see a SecurityNegotiationException when you test the service under SSL. This happens only when you use a self-signed certificate locally. There is a workaround to fix this issue - use ServerCertificateValidationCallback and return true for the local testing scenarios.
Now you are ready to test the code... Enjoy!
References:
Source code:
Note: Imported from my site https://sites.google.com/site/dhtmlexperiments/blogs/iisexpress-hostwcfserviceoverssl dated Mar 8, 2011
Comments
Post a Comment