It is common to use https communication between the phone application and the services for a secured data transportation. In order to setup https it needs a certificate. For production environment it can be purchased from a trusted CA provider such as GoDaddy. For development environment it can be generated locally by using the Microsoft Certification Creation tool makecert.exe.
Before we start doing it the environment needs to be ready. Please see my previous post Setup Windows Phone 8 Dev environment in virtual machine for this.
Let’s go through it step by step.
The self-signed certificate
Since the certificate subject name and key file name must match the website name, and the Emulator knows the development machine’s IP address, so we need to use the development machine’s IP address as the certificate subject name and key file name.
Find the development machine’s IP address for the Emulator
Fire up Visual Studio command prompt as administrator, type ipconfig then enter, two adapters should appear in the results. Pick the IP address under the Windows Phone Emulator one. In my example it is 169.254.80.80.
First we need to create a certificate to act as the root certificate authority. Still in command line windows execute the following line:
makecert -n "CN=169.254.80.80" -r -sv 169.254.80.80.pvk 169.254.80.80.cer
Input the password for the private key then press OK.
Input the password again then press OK.
Go to the folder we should be able to see two files just created, one is the certificate file the other is the private key file.
Then we need to install the root certificate authority on the development machine.
Go to the management console for certificates for local machine. Right click the Certificates folder under Trusted Root Certification Authorities, choose All Tasks -> Import...
Import the certificate file just created. After the import it should appear in the Trusted Root Certification Authorities/Certificates folder.
Then we need to create and install the temporary certificate on the development machine from the signed root CA just imported.
In command prompt execute this line
makecert -sk testcert -iv 169.254.80.80.pvk -n "CN=169.254.80.80" -ic 169.254.80.80.cer -sr localmachine -ss my -sky exchange -pe
Input the private key password then press OK and it should succeed.
Now the testcert is available for binding in IIS.
Setup a https website in IIS
In IIS add a website with the binding type https. In the SSL certificate dropdown list choose the one with the development machine IP address as the name, which was created previously.
Select the website added just now, click the Browse *.443 (https) link in the right side panel.
It should show a warning message which is expected.
Change the URL address from localhost to the IP address then it should be fine.
Now let’s create a simple WCF service and deploy it to the https website we just added.
Add a WCF Service Application project in Visual Studio.
When it is created it contains a sample service Service1.svc. We will use it for demonstration purpose.
Before publishing it to the website we added in IIS, we need to do some configurations in web.config.
In the solution explorer right click the web.config file then choose Edit WCF Configuration.
In the Configuration Editor click Create a new service for the Service1.svc and follow the wizard. Remember to choose the http and leave the endpoint address empty.
Once it is created then click Host under the new service, add a base address which is the https IP address.
Click the (Empty Name) under Endpoints, in the General tab on the right side, choose the binding type to basicHttpsBinding.
In Identity tab choose FindBySubjectName for X509FindType, then input the IP address for the FindValue.
Make sure the value for HttpsGetEnabled in ServiceMetadata settings under Service Behaviors is set to True.
Save the configuration changes then publish the WCF service to the folder was setup for the website.
Once the website is published, open the service from a browser, it should tell that the service is up and running.
Launch WCF Service Test Client. It should be located at \Microsoft Visual Studio 11.0\Common7\IDE\.
In the tool add the https service. It should be able to find the two methods defined in the service interface. Double click the GetData() method, input a value in the right side then click the button Invoke, it should receive a response with the expected value.
By now we are sure that the https service is working as expected.
Windows Phone 8 project
Let’s create a Windows Phone 8 application which invokes this https service just deployed.
Create a Windows Phone 8 project. Add a service reference to the https service.
In the MainPage.xaml add a button which points to a click method like this
Set the Windows Phone 8 project as the startup one if there are other projects in the solution.
Choose one of the Emulator from the dropdown list then press F5.
The phone application should be running in the emulator. Click the button just created and it should throw a CommunicationException. The reason is that self-signed certificate is not a trusted one in the emulator so we need to install it in the emulator manually. If the certificate is from a trusted CA then this step is not needed.
Locate the certificate added under Trusted Root Certification Authorities, right click it then choose All Tasks -> Export.
During the export wizard choose the .P7B type. Export it with a proper file name and location.
Copy the exported file to the service folder. In the browser type the url which points to the certificate file, make sure it can be downloaded successfully.
In the emulator launch IE and type the certificate file url then press enter. Tip: pressing the Pause key in your keyboard enables you to type from your keyboard in the emulator.
It should show you a warning message. Click ‘Continue to website’.
Install the certificate.
After the certificate is installed successfully, go back to IE and type the service url, it should not show any warning message again, which means the certificate is not a trusted one.
Run the phone application again and click the button, it should connect to the https service correctly this time.
As long as the emulator is not closed then the certificate stays in the emulator. When the emulator is opened next time, you need to repeat the installation process in it.