Monday, 14 October 2013

Message Security using Certificate Client Credential in WCF

Message Security using Certificate Client Credential in WCF

In this article I am explaining how to achieve message security using certificate client credential. In my last article I described the message security using user name client credential.

Following are the steps to implement message security using Certificate Client Credential:-

Step 1:-

Go to the IIS Server by typing inetmgr in run

Step 2:-

Click on server certificate



Figure 1

Step 3:-

After opening the server certificate on right panel select the create self-signed certificate



Figure 2

Now certificate creation window will be opened. Give a proper name to the certificate and click ok.







Figure 3

Step 4:-

Now create a WCF service. Go to visual studio->New->Website->Wcf Service

Step 5:-

Make changes in your web configuration file.

Add following binding tag in system.sevicemodel tag. In binding tag add message security and client credential as certificate

<bindings>
      <wsHttpBinding>
        <binding>
          <security mode="Message">
            <message clientCredentialType="Certificate"></message>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

now add behavior tag inside the service behavior tag and the certificate detail in it which you have created on IIS in the beginning.

<behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceCredentials>
            <serviceCertificate storeLocation="LocalMachine" findValue="isha" storeName="My" x509FindType="FindByIssuerName"/>
          </serviceCredentials>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

Now add service tag

<services>
      <service name="Service">
        <endpoint address="" binding="wsHttpBinding" contract="IService"/>
      </service>
    </services>

Now execute the program and you will the get following window:-



Figure 4

Now create the client and add service reference and pass client credential with all the detail of certificate.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ServiceReference1.ServiceClient svc = new ServiceReference1.ServiceClient();
     
     svc.ClientCredentials.ClientCertificate.SetCertificate(System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.X509FindType.FindByIssuerName, "isha");

        Response.Write(svc.GetData());
    }
}
Now execute the code and you will get following output
(I simply return string from service to the client)



Figure 5

if you find any problem to run this code kindly mail me your error at malhotra.isha3388@gmail.com




6 comments:

  1. Internship & Recruitment Program for MCA students
    Webtrackker also provide the 6 Month/ weeks industrial training / Internship & Recruitment Program for MCA students in Java, dot net, Web designing, web developments, Angular.js, Node.js, Hybrid apps, computer networking, Plc Scada, Auto cad, All modules in ERP sap, sap mm, sap fico. Php, Oracle Dba, networking etc for MCA, BCA, B.Tech Students.
    Webtrackker Technologies
    B-47, Sector- 64
    Noida- 201301
    Phone: 0120-4330760, 8802820025
    Email: Info@Webtrackker.Com
    Web: www.webtrackker.com

    ReplyDelete
  2. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    digital marketing company in chennai

    ReplyDelete
  3. I just see the post i am so happy to the communication science post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be replay for your great thinks and I hope you post again soon.

    digital marketing course in chennai
    software testing training in chennai

    ReplyDelete
  4. I read a weblog, I hope that it doesn't sadden me as much as this one. I’m talking about, I know it was my selection to read, but I actually thought you'd have something interesting to say. Great work admin..
    Office Interiors in Chennai
    Home Interior Decorators in Chennai

    ReplyDelete