Quantcast
Sunday 28 november 2010 7 28 /11 /Nov /2010 08:26


9 simple steps to enable X.509 certificates on WCF



Get Interview tutorials and videos on .NET 3.5, 4.0, WCF, Asp.Net, SQL Server, CAS, Security, WCF, SharePoint, Azure, OOPS and many more on www.questpond.com
 


Introduction and Goal

Beginner WCF FAQ’s

Step 1:- Create client and server certificates

Step 2 :- Copy the certificates in trusted people certificates

Step 3 :- Specify the certification path and mode in the WCF service web.config file

Step4 :- Define binding

Step5 :- Tie up the bindings with end point

Step 6 :- Make your web application client for consuming the WCF service

Step 7 :- Define certificates in WCF client

Step 8 :- Tie up the behavior with end point on WCF client

Step 9 :- Enjoy your hard work

Download code


Introduction and Goal

In this article we will discuss how we can enable certificates on WCF service. WCF has two modes by which it transfers data one is the transport and the other is the message. This tutorial will concentrate on how we can enable certificates on message mode of data transfer.

Now a days I am distributing my 400 questions and answers ebook which covers major .NET related topics like WCF,WPF,WWF,Ajax,Core .NET,SQL Server,Architecture and lot more. I am sure you will enjoy this ebook.
http://www.questpond.com/SampleDotNetInterviewQuestionBook.zip .
I have also been recording videos on .NET technologies , you can catch all the action at http://www.questpond.com



Beginner WCF FAQ’s

In case you are fresh to WCF please refer the below two WCF FAQ articles.

WCF FAQ http://www.codeproject.com/KB/aspnet/WCF.aspx  :- This is a 20 question FAQ for beginners which explains basic concepts of WCF like End points , contracts and bindings. It also discusses about various hosting methodologies of WCF service. The article finally ends talking about bindings and one ways operations in WCF.

WCF FAQ http://www.codeproject.com/KB/aspnet/WCF.aspx  :- This FAQ covers 10 questions which talks about concepts like duplex contracts , hosting WCF on different protocols , MSMQ bindings , transaction isolation levels and two way communication. The article finally ends talking about two queues volatile and dead letter queue.



Step 1:- Create client and server certificates


Create two certificates one for the server and the other for the client using makecert.exe. You can get makecert.exe from “C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin” folder. So you can goto dos prompt and run the below command snippet.

makecert.exe -sr CurrentUser -ss My -a sha1 -n CN=WCfServer -sky exchange -pe
makecert.exe -sr CurrentUser -ss My -a sha1 -n CN=WcfClient -sky exchange -pe

Below is a detailed explanation of various attributes specified in the ‘makecert.exe’.



Attribute

Explanation

-sr

Specifies the registry location of the certificate store. The SubjectCertStoreLocation argument must be either of the following:
currentUser
Specifies the registry location HKEY_CURRENT_USER.
localMachine
Specifies the registry location HKEY_LOCAL_MACHINE.

-ss

Specifies the name of the certificate store where the generated certificate is saved.

-a

Specifies the algorithm. Can be either MD5 or SHA1.

-n

Specifies a name for the certificate. This name must conform to the X.500 standard. The simplest method is to use the "CN=MyName" format.If the /n switch is not specified; the default name of the certificate is "Joe's Software Emporium".

-sky

Specifies how will be the key type. Can be either exchange or signature.

-pe

This makes the key exportable.



Note: - Makecert.exe is a free tool provided by Microsoft which helps to create X.509 certificate that is signed by a system test root key or by another specified key. This is a test certificate and not a real one and should not be used for production purpose. For production buy proper certificates from Thawte, Verisign, GeoTrust etc.
 

Currently we have specified that we want to create the client key with ‘WcfClient’ name and server key with ‘WCFServer’. The certificates should be created for the current user and should be exportable.






Once you run the command you should see the ‘Succeeded’ message as shown in the below figure. Below figure shows keys created for both server and client.



Step 2 :- Copy the certificates in trusted people certificates


Go to start à run and type MMC and press enter. You will be popped with the MMC console. Click on file à Add/remove snap-in.
You will be popped up with a Add/Remove Snap-in , click on the add button , select certificates and select ‘My user Account’.





You can see the certificates created for client and server in the personal certificates folder. We need to copy those certificates in trusted people à certificates folder.






Step 3 :- Specify the certification path and mode in the WCF service web.config file

Now that we have created both the certificates we need to refer these certificates in our WCF project.

So we have created two projects one which has the WCF service and the other project is a web application which will consume the WCF service.





Let’s open the web.config file of the WCF service and enter two important things:-
• Where the certificate is stored, location and how WCF application should find the same. This is defined using ‘serviceCertificate’ tag as shown in the below snippet.
• The ‘certificationvalidationmode’ defines how client certificates will be authenticated.




Certification validation mode

Description

Chain trust

In this situation the client certificate is validated against the root certificate.

Peer trust

PeerTrust ensures that the public key portion of the certificate is in the Trusted People certificate folder on the clients computer

ChainORPeertrust

This is just a OR condition for both chain and peer.




The above two points is clubbed together and entered in the web.config file of the WCF service.

<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="PeerTrust"/>
</clientCertificate>
<serviceCertificate findValue="WCfServer"
storeLocation="CurrentUser"

storeName="My"
x509FindType="FindBySubjectName" />
</serviceCredentials>



Step4 :- Define bindings

Now that we have defined our certificates and authentication type we need to define that the authentication values will be sent through message using certificates. You can see we have defined the ‘WsHttpBinding’ with message attribute specifying that the WCF client needs to send a certificate for validation.

<bindings>
<wsHttpBinding>
<binding name="wsHttpEndpointBinding">
<security>
<message clientCredentialType="Certificate" />
</security>

</binding>
</wsHttpBinding>
</bindings>



Step5 :- Tie up the bindings with end point


Once done we need to tie up this binding with the end point. This is done by using ‘bindingConfiguration’ tag as shown in the below code snippet.

<endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" contract="WCFServiceCertificate.IService1">

Step 6 :- Make your web application client for consuming the WCF service


That’s all we need to from the WCF service perspective. So compile the WCF service and reference the same in the ASP.NET web application using ‘Service reference’. Below is the code snippet where we have referenced the service and called the ‘GetData’ function of the service/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebConsumer.ServiceReference1;
namespace WebConsumer
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Service1Client obj = new Service1Client();
Response.Write(obj.GetData(12));
}
}
}

Now if you try to run the client i.e. the web application as it is you should get an error as shown below. The error clearly indicates you can not use the WCF service until you do not provide the client certificate.






Step 7 :- Define certificates in WCF client

So lets start the process of defining certificates in the WCF client. The way we have defined authentication certification mode and the path of the certificate, in the same way we need to define it for WCF client. You can see we have defined the authentication mode as ‘peertrust’ and we have specified the client certificate name as ‘WcfClient’.

<behaviors>
<endpointBehaviors>
<behavior name="CustomBehavior">
<clientCredentials>
<clientCertificate findValue="WcfClient" x509FindType="FindBySubjectName" storeLocation="CurrentUser" storeName="My" />

<serviceCertificate>
<authentication certificateValidationMode="PeerTrust"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>


Step 8 :- Tie up the behavior with end point on WCF client


We need to tie up the above defined behavior with the end point. You can see we have bounded the behavior using ‘behaviorConfiguration’ property. We also need to specify that the DNS value will be ‘WcfServer’ which your server certificate name..

<client>
<endpoint address="http://localhost:1387/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"

name="WSHttpBinding_IService1" behaviorConfiguration="CustomBehavior">
<identity>
<dns value="WcfServer" />
</identity>
</endpoint>

</client>



Step 9 :- Enjoy your hard work

Once we are done you can run the ASP.NET web and you should see the below display.




Download code

You can download both the server and client code http://www.codeproject.com/KB/WCF/9StepsWCF/WCFCertificate.zip


By Shivprasad koirala
Enter comment - View the 0 comments
Home

Important .NET and C# interview questions and answers

.Net interview questions: - Explain why it is not preferred to use finalize for clean up?

.Net interview questions: - Show the five levels in CMMI?

.NET interview questions and answers: – Which is the best place to store connection string in .NET projects?

C# interview questions and answers: – Explain the use of Icomparable in c#?

C# interview questions: - How can we check which rows have changed since dataset was loaded?

C# interview questions and answers: - Can you write a simple c# code to display Fibonacci series?

.NET interview questions and answers: - What is difference betweenIcomparable VS Icomparer ?

C# and .NET interview question: -What is short circuiting in C#?

C# and .NET interview question: - What are symmetric and asymmetric algorithms?

Important c# and .NET interview question on object pooling and Gridview events?

.NETinterview questions and answers: – Will the finally run in this code?

How to prepare for c# and .NETinterviews?

C# and .NET Interview questions: - What is Thread.Join () in threading?

.NET Interview questions and answers: -What is serialization and deserialization in .NET?

C# and .NET interview question: - What is hashing?

c# and .NET interview question:- what connects dataset and data source ?

.Net interview questions and answers: - What is the difference between “Web.config” and “Machine.Config”?

.NET interview questions and answers: - What is TPL?

.NET Interview questions and answers: -What are different access modifiers?

.NET and c# Interview Question and answers: – If we want to update interface with new methods, what is the best practice?

 MVC ( Model view controller) interview questions and answers      

ASP.NET Application and Page Life Cycle 

12 Important FAQ’s on VSTS Testing (Unit testing, load testing, automated testing, database testing and code coverage) 

 6 important use of Partial/Mock testing

6 important uses of Delegates and Events

7 Simple Steps to Run Your First Azure Blob Program

8 Steps to Create Workflows using SharePoint Designer

Azure FAQ Part 1

C# Code Reviews using StyleCop – Detailed Article

Four real world uses of Partial classes and Partial methods

SharePoint Quick Start FAQ Part 1

SharePoint Quick Start FAQ Part 6 – Workflows, Workflows and Workflows

SharePoint Workflow Basics

 

 

 

 

. NET and C# interview questions videos

Algorithm Interview Questions

Algorithm interview questions and answers: – Can you write code for bubble sort algorithm?

Algorithm interview questions and answers: – What is inserted sort algorithm?

ASP.NET Interview Questions & Answers Article







 

SQL Server Interview Questions & Answers Article

 

SQL Server Interview Questions & Answers Article

SQL Server interview questions and answers: - What is HID data type in SQL Server ?

 

.NET INTERVIEW QUESTIONS & ANSWERS ARTICLE

 

.NET interview questions and answers: - How to reverse a string in .NET ( DotNet)?

.NET interview questions and answers: - What is the use of Click Once?

.NET interview questions and answers: - Will the below codes create new instances?

C# and .NET interview questions with answers – What is Nuget?

Dependency injection (DI) VS Inversion of Control (IOC)

.NET interview questions with answers: - What is the difference between Reflection and Dynamic?


WPF INTERVIEW QUESTIONS & ANSWERS ARTICLE

 

6 important WPF and Silverlight Multi-threading interview questions with answers


Create your blog for free on over-blog.com - Contact - Terms of Service - Earn Royalties - Report abuse - Most commented articles