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




12 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 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
  4. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic.
    SEO Company in India

    ReplyDelete
  5. Your thinking toward the respective issue is awesome also the idea behind the blog is very interesting which would bring a new evolution in respective field. Keep update more information.
    Industrial Architecture
    Warehouse Architect
    Civil Engineering Consultants
    Office Interiors in Chennai

    ReplyDelete
  6. Fertility is the natural capability to produce offspring. As a measure, fertility rate is the number of offspring born per mating pair, individual or population.Human fertility depends on factors of nutrition, sexual behavior, consanguinity, culture, instinct, endocrinology, timing, economics, way of life, and emotions.Greate thinks of a fertility center for humans.

    Fertility Center in OMR

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. thanks for your post Here is STUCORNER the Best DIGITAL marketing training institute in delhi you can visit their site:Stucorner

    ReplyDelete