Click here to Skip to main content
Licence CPOL
First Posted 28 Apr 2008
Views 99,213
Downloads 846
Bookmarked 57 times

Simple WCF - X509 Certificate

By | 28 Apr 2008 | Article
how to create Temporary X509 certificate and also to implement X509 Certificate security in WCF service and client

Introduction

This article will describe how to create Temporary X509 certificate and to implement X509 Certificate security in WCF service and client. This configuration can be simply done via in config file itself.

Using the Code

Creating X509 Certificate:

makecert.exe this tool will helpful to create X509 certificate. This tool is packed along with Microsoft .Net 2005 SDK and also be downloaded from micosoft site.

Step1: creating the temporary certificate TempCA.cer
Step2: Create SignedByCA.cer which is digitally sign and authorize by TempCA certificate
Step3: Import the certificate TempCA.cer using MMC in to "Trusted Root Certificate Authorities" folder (Localmachine)
Step4: Import the certificate SignedByCA.cer in to the personal folder

step1: makecert -n "CN=TempCA" -r -sv TempCA.pvk TempCA.cer
step2: makecert -sk SignedByCA -iv TempCA.pvk -n "CN=SignedByCA" -ic TempCA.cer SignedByCA.cer -sr localmachine -ss My

step3: Go to Start -> Run -> Type MMC -> File -> Add/Remove Snap-In -> StandAlone Tab -> Add Button -> Certificates -> Computer Account

step4: Import SignedByCA.cer in to Personal Folder




Export Certificate:To export the created certifciate to outside world, it is necessary to export the private key also. Thus it is possible to convert the certificate .cer and .pvk to .pfx (i.e TempCA.cer and TempCA.pvk to TempCA.pfx). Equivalent tool for this conversion is pvk2pfx.exe. This will be available in your <programfile>/Visual Studio 8/Common7/Tools/bin/pvk2pfx.exe

cmd> pvk2pfx.exe -pvk TempCA.pvk -spc TempCA.cer      
this open wizard which will help to create .pfx file. Then you can distribute the PFX file to client

Access Permission X509 certificate:
If WCF services are hosted in IIS or Windows service or so on, based on the hosted environment specific permission to be give for X509 certificate. For ex: if WCF service is hosted in IIS then ASPNET user (XP) permission must be given to the certifciate. Thus, this can be achived using Winhttpcertcfg.exe which will give the permission to specified certificate. Seperate download is available for this tool and also this tool is downloadable along this article itself.

//for grant ASPNET permission to TempCA.cer
cmd>winhttpcertcfg -g -c LOCAL_MACHINE\MY -s TempCA -a ASPNET

//for grant ASPNET permission to TEMPCA.pfx
cmd>winhttpcertcfg -i TempCA.pfx -g -c LOCAL_MACHINE\My -s TempCA -a ASPNET     
        
WCF Server Config:

Configure the WCF service to support X509 certificate as one of the security process. possible to configure the client level certificate authority in the server config file itself. Thus at the time of creating proxy class using svcutil.exe config file will be generated with certificate information (token).

//web.config
<configuration>
<system.serviceModel>  
<service name="service1" behaviorConfiguration="beh1">
         <endpoint address="secure" contract="Icontract1" binding="wsHttpBinding" bindingConfiguration="binding1"></endpoint>
         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<behaviors>
    <serviceBehaviors>
         <behavior name="beh1">
              <serviceDebug includeExceptionDetailInFaults="true"/>
              <serviceMetadata httpGetEnabled="true"/>
              <serviceCredentials>
                <!--certificate storage path in the server -->
                <serviceCertificate findValue="TempCA" x509FindType="FindBySubjectName" storeLocation="LocalMachine"  storeName="My"/>
                <issuedTokenAuthentication allowUntrustedRsaIssuers="true"/>
                <!--certificate storage path in the client -->
                <clientCertificate>
                  <certificate findValue="TempCA" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My"/>
                </clientCertificate>                
                <userNameAuthentication userNamePasswordValidationMode="MembershipProvider"/>
              </serviceCredentials>
         </behavior>
     </serviceBehaviors>
     <endpointBehaviors>
        <behavior name="beh1">
          <clientCredentials>
            <!--certificate storage path in the client -->
            <clientCertificate findValue="TempCA" storeLocation="LocalMachine" x509FindType="FindBySubjectName" storeName="My"/>
            <serviceCertificate>
              <authentication certificateValidationMode="PeerOrChainTrust"/>
            </serviceCertificate>
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
</behaviors>
<bindings>
     <wsHttpBinding>
        <binding name="binding1">
          <security mode="Message">
            <!--security mode of certificate -->
            <message establishSecurityContext="false" clientCredentialType="Certificate"/>
          </security>
        </binding>        
      </wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>

Client Config:(Auto generated using svcutil.exe)

Sample WCF client config file generated using svcutil.exe

<!--app.config-->
    <client>
      <endpoint address="http://localhost/service1/servic1.svc/secure"
        binding="wsHttpBinding" bindingConfiguration="bind1"
        contract="Icontract1" name="WSHttpBinding_Icontract1">
        <identity>
          <certificate encodedValue="AwAAAAEAAAAUAAAAOTDk6LO4LsMQaY+65EgACb==" />
        </identity>
      </endpoint>      
    </client>      
    

After successful implementation of above points, when any request send from client to the server then the message will be digitally signed by the certifciate present in the client and the server

Points of Interest

I have interested to start security implementation with WCF

History

April 29, 2008. First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

meetsenthilbtech

Technical Lead
Infosys Technologies
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow .Net call a self-sign java web service Pinmemberlpbinh22:43 11 Mar '12  
QuestionWhere is SignedByCA used? PinmemberRajeshjoseph3:58 21 Jan '12  
QuestionSOAP Security negociation FAIL !! PinmemberMember 77585312:40 16 Mar '11  
GeneralASPNET account could not be found Pinmembercilker3:11 2 Feb '11  
GeneralTest Certificate versus Production Certificate PinmemberDotNet_Naeem15:10 28 Aug '09  
GeneralRe: Test Certificate versus Production Certificate PinmemberDotNet_Naeem6:21 31 Aug '09  
GeneralI only require signing of messages PinmemberRoss100006:14 18 Sep '08  
GeneralRe: I only require signing of messages [modified] Pinmembermeetsenthilbtech22:43 6 Jan '09  
General'Temporary' Certificate Pinmemberrctaubert14:35 9 Aug '08  
GeneralRe: 'Temporary' Certificate Pinmembermeetsenthilbtech22:40 6 Jan '09  
GeneralRe: 'Temporary' Certificate PinmemberLoona704:18 15 Sep '09  
Questionwinhttpcertcfg does not work, Access Denied PinmemberRedCatsRic4:28 4 Aug '08  
AnswerRe: winhttpcertcfg does not work, Access Denied Pinmembermeetsenthilbtech22:39 6 Jan '09  
GeneralRe: winhttpcertcfg does not work, Access Denied PinmemberJohn Kenedy S.Kom21:53 20 Sep '11  
GeneralBig help for using wcf with certs from an asp.net page! PinmemberCorgalore9:32 29 Jul '08  
GeneralGreat article! But.. PinmemberDavidPrice6:38 10 Jun '08  
AnswerRe: Great article! But.. Pinmembermeetsenthilbtech19:15 10 Jun '08  
GeneralRe: Great article! But.. Pinmembercarlosnajam21:52 18 Jul '10  
GeneralNice but have one question PinmemberSalam Y. ELIAS7:18 29 May '08  
AnswerRe: Nice but have one question Pinmembermeetsenthilbtech18:27 29 May '08  
GeneralRe: Nice but have one question PinmemberNisd23:24 3 Jan '10  
Questiondownloadable source code PinmemberdestructoDave17:41 12 May '08  
Question.net support PinmemberLoci20:38 5 May '08  
AnswerRe: .net support Pinmembermeetsenthilbtech20:58 5 May '08  
GeneralThank you for tutorial Pinmemberaclar11:02 30 Apr '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 29 Apr 2008
Article Copyright 2008 by meetsenthilbtech
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid