Skip to main content
 
Go Search
Home
Categories
Bloggers
HttpWebRequest with Client Certificate Authentication
By: Dave Scheele | Posted: September 30, 2006 at 8:03 AM

The .NET Framework HttpWebRequest permits the developer to access resources on a server using the HTTP or HTTPS protocols. Some very secure systems, however, require a client X509 certificate as evidence to access resources. Setting this up in an ASP.NET application is not straightforward because the default ASP.NET service account has limited permissions and does not have the required access to the local certificate store.

The basic idea is to add the desired X509Certificate to the ClientCertificates collection on the HttpWebRequest object before calling the GetResponse method. Creating the X509Certificate object requires the private key of the certificate first be exported to a file in the file system which acts as the input to the X509Certificate::CreateFromCertFile static method. However, in order to successfully access the certificate in the certificate store, it is necessary to execute the code under the account used to install the certificate. We accomplish this by running the code as a COM+ serviced component, configuring the component to run using the credentials of the account under which the certificate was installed.

The example I give here is doing an HTTP POST to the server, attaching the X509 certificate before open the request stream, writing the POST data, and requesting the response.

First, here is the method used to attach the certificate to the HttpWebRequest:

/// <summary>
/// Attach an X509 client certificate to an existing Http request.
/// </summary>
/// <param name="request">The Http request to which to attach the client certificate.</param>
/// <param name="userName">The user account name (machinename\username) under which the client certificate was installed.</param>
/// <param name="certPath">The path to the exported client certificate file.</param>
protected void AttachClientCertificate( HttpWebRequest request, string userName, string certPath )
{
    TraceWriteLine( "Attaching X509 certificate to HttpWebRequest" );

    X509Certificate certificate = ( X509Certificate) _certificates[ certPath ];
    if ( certificate == null )
    {
        TraceWriteLine( "X509 certificate not in cache: Creating from file and caching" );
        certificate = X509Certificate.CreateFromCertFile( certPath );
        _certificates.Add( certPath, certificate );
    }
    request.ClientCertificates.Add( certificate );

    TraceWriteLine( "X509 certificate successfully attached to HttpWebRequest" );

}

And here is the method used to do the HTTP POST, which makes use of the AttachClientCertificate method:

/// <summary>
/// Posts data to specified Uri using HTTP POST, optionally attaching an X509 client certificate in the process.
/// </summary>
/// <param name="requestUri">The Uri to post data.</param>
/// <param name="postData">The data to be posted.</param>
/// <param name="userName">The user account name (machinename\username) under which the client certificate was installed.</param>
/// <param name="certPath">The path to the exported client certificate file.</param>
/// <returns>The response from the remote host.</returns>
/// <remarks>
/// A client certificate is attached only if the Uri scheme is https and the client certificate path is supplied.
/// It is assumed that if the certificate path is supplied, the certificate user account name is also supplied.
/// </remarks>
public string GetResponse( string requestUri, string postData, string userName, string certPath )
{
    TraceWriteLine( "HttpWebRequestor.GetResponse(string,string,string,string): Entered" );

    try
    {
        ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy( );

        // Create the request
        HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create( requestUri );

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postData.Length;

        // Attach the client certificate if https and certPath specified.
        if ( new Uri( requestUri ).Scheme == Uri.UriSchemeHttps && certPath != "" )
         {

            AttachClientCertificate( request, userName, certPath );
         }

        // Write data to request
        StreamWriter requestWriter = new StreamWriter( request.GetRequestStream( ) );
        try
        {
            requestWriter.Write( postData );
        }
        finally
        {
            requestWriter.Close( );
        }

        // Send to the remote server and wait for the response
        HttpWebResponse response = ( HttpWebResponse ) request.GetResponse( );

        // Read the response
        string responseString;
        StreamReader responseReader = new StreamReader( response.GetResponseStream( ) );
        try
        {
            responseString = responseReader.ReadToEnd( );
        }
        finally
        {
         responseReader.Close( );
        }

        // Return response
        return responseString;
    }
    catch ( Exception e )
    {
        throw;
    }
    finally
    {
        TraceWriteLine( "HttpWebRequestor.GetResponse(string,string,string,string): Exiting" );

        LogMessage( _trace.ToString( ) );
    }

}


 


  Comments   Add Comment   Share It  
  Your Name:
  Your Email: **will not be displayed
  Comment Title:
* Comments:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
   
  
  
* Your Name:
* Your Email: **will not be displayed
* Recipient's Email:
* Subject:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
  
  
  
Watches
By: Watches | Posted: July 12, 2010 at 3:32 AM
<a href="http://www.louisvuittonme2.com/">Choo Handbags</a> <a href="http://www.dropshippinghandbags.com/">Balenciaga Handbags</a> <a href="http://www.louisvuitton2you.com/">Mulberry Handbags</a> <a href="http://www.dhgatewholesale.net/">Jacobs Handbags</a> <a href="http://www.watchesnew2010.com/">Tag Heuer Watches</a> [url=http://www.replicawatches58.com/]Wholesale fashion watches[/url] [url=http://www.wholesalefakewatches.com/]Designer watches[/url] [url=http://www.fakewholesalewatches.com/]Bvlgari Watches[/url] [url=http://www.edmundchein.net/]Rolex Watches[/url] [url=http://www.chanel2handbags.com]Cartier Handbags[/url]
replica watches
By: replica watches | Posted: June 17, 2010 at 5:03 AM
<p><a href=http://www.bagswatches.com/>replica watches</a> on sale. You can buy the cheapest <a href= http://www.bagswatches.com/replica-rolex-watches.html >replica Rolex</a> <a href= http://www.bagswatches.com/replica-rolex-watches.html >cheap rolex</a> <a href= http://www.bagswatches.com/replica-swiss-watches.html >Swiss Replica watches</a> <a href= http://www.bagswatches.com/replica-swiss-watches.html >Swiss watches</a> <a href= http://www.bagswatches.com/replica-Swiss-Rolex-watches.html >Swiss Rolex</a> or Free shipping. We carry all <a href= http://www.bagswatches.com/ >replica handbags</a> brands,<a href= http://www.bagswatches.com/ >knock off handbags replica handbags</a> <a href= http://www.bagswatches.com/ >fake handbags</a> <a href= http://www.bagswatches.com/replica-Louis-Vuitton-handbags.html >knock off Louis Vuitton</a> <a href= http://www.bagswatches.com/replica-Louis-Vuitton-handbags.html >Louis Vuitton handbags</a> <a href= http://www.bagswatches.com/replica-Louis-Vuitton-handbags.html >replica Louis Vuitton handbags</a> <a href= http://www.bagswatches.com/replica-Louis-Vuitton-handbags.html >replica Louis Vuitton</a> <a href= http://www.bagswatches.com/replica-Gucci-handbags.html >Gucci handbags</a> <a href= http://www.bagswatches.com/replica-Gucci-handbags.html >knock off gucci handbags</a> <a href= http://www.bagswatches.com/replica-Prada-handbags.html >Prada handbags</a> <a href= http://www.bagswatches.com/replica-Gucci-handbags.html >fake Gucci handbags</a> <a href= http://www.bagswatches.com/replica-Hermes-handbags.html >Hermes handbags</a> <a href= http://www.bagswatches.com/replica-Burberry-handbags.html >Burberry handbags</a> <a href= http://www.bagswatches.com/replica-Chanel-handbags.html >chanel handbags</a> <a href= http://www.bagswatches.com/replica-Chanel-handbags.html >replica Chanel handbags</a> Looking for Cheap and <a href= http://www.bagswatches.com/ >Discount Handbags</a> <a href= http://www.bagswatches.com/ >Designer handbags</a> <a href= http://www.bagswatches.com/replica-Louis-Vuitton-handbags.html >Louis Vuitton handbags</a> and many more <a href= http://www.bagswatches.com/ >designers</a> Cheap<a href= http://www.bagsreplicashop.com/ > Replica Designer Handbags</a> For Sale! <a href= http://www.bagsreplicashop.com/ >knock off handbags</a> <a href= http://www.bagsreplicashop.com/ >replica handbags</a> <a href= http://www.bagsreplicashop.com/ >replica bags</a> <a href= http://www.bagsreplicashop.com/replica-Wallets.html >wallet</a>s <a href= http://www.bagsreplicashop.com/replica-Louis-Vuitton-bags.html >Louis Vuitton handbags</a> of top most designer. <a href= http://www.bagsreplicashop.com/replica-Louis-Vuitton-bags.html >replica Louis Vuitton handbags</a> <a href= http://www.bagsreplicashop.com/replica-Louis-Vuitton-bags.html >fake Louis Vuitton handbags</a> <a href= http://www.bagsreplicashop.com/replica-Gucci-bags.html >Gucci handbags</a> <a href= http://www.bagsreplicashop.com/replica-Gucci-bags.html >Gucci bags</a> <a href= http://www.bagsreplicashop.com/replica-Gucci-bags.html >replica Gucci handbags</a> Online China replica Goods offer cheapest designer <a href= http://www.bagsreplicashop.com/replica-Prada-bags.html >Prada handbags</a> <a href= http://www.bagsreplicashop.com/replica-Prada-bags.html >replica Prada handbags</a> <a href= http://www.bagsreplicashop.com/replica-Hermes-bags.html >Hermes handbags</a> <a href= http://www.bagsreplicashop.com/replica-Burberry-bags.html >Burberry handbags</a> <a href= http://www.bagsreplicashop.com/replica-Chanel-bags.html >Chanel handbags</a> <a href= http://www.bagsreplicashop.com/replica-Chanel-bags.html >replica Chanel handbags</a> most of them are genuine leather <a href= http://www.bagsreplicashop.com/ >Designer handbags</a> <a href= http://www.bagsreplicashop.com/ >Discount handbags</a> <a href= http://www.bagsreplicashop.com/ >cheap handbags</a> <a href= http://www.bagsreplicashop.com/ >replica handbags</a> on sale.Order 2010 <a href= http://www.bagsreplicashop.com >Designer Handbags</a>.We have tons of sale <a href= http://www.linkreplica.com/ >replica watches</a> <a href= http://www.shopping-replica.com/ >replica handbags</a> keen sale,<em><a href= http://www.linkreplica.com/replica-watches-rolex-watches-c-409_411.html >replica rolex</a> </em><a href= http://www.linkreplica.com/replica-watches-c-409.html >replica watches</a> <a href= http://www.linkreplica.com/replica-watches-rolex-watches-c-409_411.html >rolex replica</a> with free shipping.<a href= http://www.linkreplica.com/replica-watches-rolex-watches-c-409_411.html >cheap rolex</a> <a href= http://www.linkreplica.com/replica-watches-rolex-watches-c-409_411.html >fake rolex</a> <a href= http://www.linkreplica.com/replica-watches-rolex-watches-c-409_411.html >rolex watches</a> <a href= http://www.linkreplica.com/replica-watches-rolex-watches-c-409_411.html >replica rolex watches</a> <a href= http://www.linkreplica.com/ >Swiss watches</a> <a href= http://www.linkreplica.com/Swiss-Rolrx.html >Swiss Rolex</a> <a href= http://www.linkreplica.com/ >replica watches</a> <a href= http://www.oemwatches.com/ >replica watches</a> <a href= http://www.thechinaphone.com/ >replica handbags</a> <a href= http://www.watch-well.com/ >replica watches</a> <a href= http://www.watchescentre.com/ >replica watches</a> and Many More Luxury Brand <a href= http://www.shopping-replica.com/ >Replica handbags</a> <a href= http://www.shopping-replica.com/ >knock off handbags</a> <a href= http://www.shopping-replica.com/ >fake handbags</a> <a href= http://www.shopping-replica.com/replica-Louis-Vuitton.html >knock off Louis Vuitton</a> <a href= http://www.shopping-replica.com/replica-Louis-Vuitton.html >Louis Vuitton handbags</a> <a href= http://www.shopping-replica.com/replica-Louis-Vuitton.html >replica Louis Vuitton handbags</a> <a href= http://www.shopping-replica.com/replica-Louis-Vuitton.html >fake Louis Vuitton handbags</a> <a href= http://www.shopping-replica.com/replica-Gucci.html >Gucci handbags</a> <a href= http://www.shopping-replica.com/replica-Gucci.html >Gucci bags</a> <a href= http://www.shopping-replica.com/replica-Gucci.html >replica Gucci handbags</a> <a href= http://www.shopping-replica.com/replica-Gucci.html >fake Gucci handbags</a> <a href= http://www.shopping-replica.com/replica-Gucci.html >knock off gucci handbags</a> <a href= http://www.shopping-replica.com/replica-Prada.html >Prada handbags</a> <a href= http://www.shopping-replica.com/replica-Prada.html >replica Prada handbags</a> <a href= http://www.shopping-replica.com/ >Designer handbags</a> Discount handbags <a href= http://www.shopping-replica.com/ >replica handbags</a> <a href= http://www.shopping-replica.com/replica-Chanel.html >chanel handbags </a>Finest Workmanship,Quality Guaranteed</p> <p>&nbsp; </p> </body> </html>
<a href=http://www.watchesday.com>replica watches</a>
By: <a href=http://www.watchesday.com>replica watches</a> | Posted: May 27, 2010 at 10:31 PM
dd
<a href=http://www.watchesday.com>replica watches</a>
By: <a href=http://www.watchesday.com>replica watches</a> | Posted: May 27, 2010 at 10:31 PM
The new Piaget [url=http://www.watchesview.com/rolex-watches.html]replica watches[/url] Possess
replica wathces
By: replica wathces | Posted: May 9, 2010 at 8:20 AM
<a href=http://www.pocwatches.com>replica watches</a> <a href=http://www.corwatches.com>replica watches</a> <a href=http://www.watchesfan.com>replica watches</a> <a href=http://www.sanwatches.com>replica watches</a>
cect p168
By: agoodic | Posted: November 9, 2008 at 6:55 PM
http://www.agoodic.com
How to call a Web service by using a client certificate for authentication in an ASP.NET Web application
Posted: July 13, 2007 at 3:32 PM
I also created a walkthrough: http://igottools.com/Lists/Posts/Post.aspx?ID=11
 

 About Dave Scheele

Professional Services ManagerDave Scheele is the professional services manager for PointBridge. He has over 15 years of experience in IT consulting, working with clients in a variety of industries, including banking, communicatio... [more]

 Tag Cloud

 External Links

 ‭(Hidden)‬ Admin Links