Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Securing Web Services with X.509 Certifica...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Securing Web Services with X.509 Certificates
By: MSDN Virtual Labs
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-07-20

    Table of Contents:
  • Securing Web Services with X.509 Certificates
  • Signing with a Certificate
  • Encrypting the Body with a Certificate
  • Encrypting a UsernameToken with a Certificate

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Securing Web Services with X.509 Certificates - Signing with a Certificate


    (Page 2 of 4 )

    Once you have the certificates installed on your computer, you can begin using them to sign and encrypt messages. In this step, you're going to sign messages sent from the client application using the client certificate (found in the Current User store). 

    1. Return to Visual Studio .NET 2003
    2. Click File | Open | Project.
    3. Navigate to C:\Microsoft Hands-on-Lab\DEV-HOL34\VB \ Exercises\B\before
    4. Select SecureInvoiceB.sln and click Open.

      Note: this solution is equivalent to the one you implemented in the last exercise. It uses a UsernameToken to authenticate, sign, and encrypt message. 
    5. Open the code view of InvoiceManagerForm.vb in the SecureInvoiceClient project.
    6. Add an Imports statement for Microsoft.Web.Services2. Security.X509:

      ' InvoiceManagerForm.vb
      ...
      Imports
      Microsoft.Web.Services2. Security. X509
      ...

    7. Add a new method to the InvoiceManagerForm class named GetX509Token. The method should take two strings (one to represent certificate's key identifier and another to represent the certificate store name that you're going to retrieve the certificate from), and return an X509SecurityToken as illustrated here:

      ...
      Private Function GetX509Token(ByVal keyId As
      String,
          ByVal storeId As String) As X509SecurityToken
          ...
      End Function 'GetX509Token
      ...

    8. Within the GetX509Token method, you need to retrieve an X509CertificateStore object for the specified store, open the store for reading, and find the certificate based on its key identifier. Then, return the identified certificate as a new X509SecurityToken object. Here's one way to write this code:

      ...
      Private Function GetX509Token(ByVal keyId As String,
          ByVal storeId As String) As X509SecurityToken
          Dim store As X509CertificateStore = X509CertificateStore.Current UserStore(storeId) 
          store.OpenRead()
          Dim certs As X509CertificateCollection =    
             store.FindCertificate ByKeyIdentifier( 
               Convert.FromBase64 String(keyId))
          store.Close()
          Return New X509SecurityToken(CType(certs(0),
      X509Certificate))
      End Function 'GetX509Token
      ...
       
    9. Go to the ConfigureProxy method in InvoiceManagerForm. This is where we need to modify the code to use the client certificate (found in the Current User store) for signing the message.
    10. Within ConfigureProxy, remove all code within the function. You'll be replacing this code with certificate signing and encryption. You'll be replacing this code with certificate signing and encryption.
    11. Within ConfigureProxy, call your new GetX509Token method. For the key identifier, specify "gBfo0147lM6cKnTbbMSuMVvm FY4=", and for the store name, specify X509CertificateStore.MyStore as illustrated here:

       ...
      ' Retreive client certificate for signing Dim clientToken As X509SecurityToken = GetX509Token(   
       "gBfo0147lM6cKnTbbMSuMVvm
      FY4=", X509CertificateStore.MyStore)
      ...


      Note: You can use the X509 Certificate Tool (called WseCertificate2.exe) to determine the key identifier for a given certificate. This tool ships with WSE 2.0. You can find it in the following directory: C:\Program
      Files\Microsoft WSE\v2.0\Tools\Certificates
      . Here's what it looks like:

       

    12. Add the returned X509SecurityToken to the proxy's RequestSoapContext.Security.Tokens collection as illustrated here:

      ...
      ' Retreive client certificate for signing
      Dim clientToken As X509SecurityToken = GetX509Token( _  
         "gBfo0147lM6cKnTbbMSuMVvm FY4=", X509CertificateStore.MyStore)
      ' Add UsernameToken for authentication purposes proxy.RequestSoapContext. Security.Tokens.Add(login.
      Token)
      ' Must add client token to message for signature processing proxy.RequestSoapContext. Security.Tokens.Add(client
      Token)
      ...


      Note: You should continue to send the UsernameToken as well. The UsernameToken will still be used for authentication and authorization purposes. Now you're going to use the certificate for signing the message. 

    13. Instantiate a new MessageSignature based on the X509SecurityToken and add it to the proxy's RequestSoapContext.Security. Elements collection as illustrated here:

       ...
      ' Retreive client certificate for signing
      Dim clientToken As X509SecurityToken = GetX509Token( _  
         "gBfo0147lM6cKnTbbMSuMVvm FY4=", X509CertificateStore.MyStore) ' Add UsernameToken for authentication purposes proxy.RequestSoapContext. Security.Tokens.Add(login.
      Token)
      ' Must add client token to message for signature processing proxy.RequestSoapContext. Security.Tokens.Add(client
      Token)
      proxy.RequestSoapContext. Security.Elements.Add( _  
         new MessageSignature(clientToken))
      ...
       
    14. Open WseSecurityHelpers.vb in the SecureInvoiceServiceB project and comment out the call to CheckForEncryption in GetUsernameToken. This makes it so the service doesn't require encryption for the time being.

      Note: you'll add X.509-based encryption in the next step and add this line of code back in.

    15. Right-click on the SecureInvoiceServiceB project in Solution Explorer and click WSE Settings 2.0. Navigate to the Security tab and check Allow Test Roots in the X.509 Certificate Settings section.
    16. Click Yes to confirm the enabling of test roots. 
    17. Press OK to close the tool.
    18. Build the solution and run the client application. Verify that if everything works as before (except for requiring encryption). 
    19. Open OutputTrace.webinfo in the SecureInvoiceClient output directory (bin).

      Notice that the message now contains a BinarySecurityToken element in addition to the UsernameToken from before.

     

    More Visual Basic.NET Articles
    More By MSDN Virtual Labs


       · We hope you found this exercise from MSDN Virtual Labs to be educational and...
     

    VISUAL BASIC.NET ARTICLES

    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway