How to call a Web service by using a client certificate for authentication in an ASP.NET Web application
On This PageSUMMARYThis article discusses how to pass a client certificate to a Web service for authentication from an ASP.NET Web application. Note The steps in this article also apply to when you make a direct HTTP request by using the HttpWebRequest class if you do not invoke a Web service. INTRODUCTIONFrequently, Web services must authenticate
applications that call the Web services. The Web services must perform authentications of calling applications before the Web services can perform authorizations. One
authentication technique is to require applications that call the Web service to present a
client certificate. When an ASP.NET Web application tries to call a Web service that uses certificate authentication, you may receive an "access denied" error message. When a console application or a Microsoft Windows Forms application calls the same Web service, you do not receive an error message. This behavior occurs because the computer maintains two different certificate stores:
MORE INFORMATIONTo enable an ASP.NET Web application to use a client
certificate, you must install the client certificate in the local machine store.
When you install a client certificate in the local machine store,
the client certificate is only available for user accounts in the Administrators group
and for the user who installed the client certificate. Therefore, you must grant access to the client certificate for the user account that is used to run
the ASP.NET Web application. Note You must have the Microsoft .NET Framework 1.1 Service Pack 1 (SP1) installed to use client certificates in the local machine store. Additionally, when the ASP.NET Web application calls the Web service, the application must export the client certificate from the certificate store and then add the client certificate to the Web service call. Install the client certificate and grant access for the user accountTo install the client certificate, and to grant access to the client certificate for the user account that is used to run the ASP.NET Web application, follow these steps.Step 1: Install the client certificate in the local machine storeIf you have a client certificate in a PKCS#12 (.pfx) file, you can use the Microsoft Windows HTTP Services Certificate Configuration Tool (WinHttpCertCfg.exe) to install the client certificate and to grant access to the client certificate for additional user accounts such as the Network Service account. To do this, follow these steps:
Step 2: Configure access to the client certificateIn this step, you must grant permission for the ASP.NET account to access the client certificate that is stored in the local machine store. The Network Service account is the default account for running Web applications on Windows Server 2003. Therefore, you must grant access to the certificate for the Network Service account. If you have configured a custom account to run ASP.NET, you must grant access for the custom account.Note In Microsoft Internet Information Server (IIS) 5.0, ASP.NET runs under the ASPNET account and not under the Network Service account. Therefore, you must to grant permissions for the ASPNET account on a computer that is running IIS 5.0. To grant access for a specific user account, run the following command at a command prompt: WinHttpCertCfg.exe -g -c LOCAL_MACHINE\MY -s "IssuedToName" -a "AccountName" Note AccountName is
the name of a local computer account or a domain account. IssuedToName is the name of the company or domain to which the client certificate
was issued. This command contains a case-insensitive search string. The search string finds the first
enumerated certificate that has a subject name that contains the string.The following command-line command is an example of how to grant access to the client certificate for the Network Service account in Microsoft Internet Information Services (IIS) 6.0: WinHttpCertCfg.exe -g -c LOCAL_MACHINE\MY -s "IssuedToName" -a "NetworkService" The following command-line command is an example on how to grant access to
the client certificate for the ASPNET account in IIS 5.0:WinHttpCertCfg.exe -g -c LOCAL_MACHINE\MY -s "IssuedToName" -a "ASPNET" Note When you use the Windows HTTP
Services Certificate Configuration Tool, you can combine the
process of importing the client certificate and the
process of configuring access to the client certificate in one step. For
example, the following command-line command performs both processes:Winhttpcertcfg.exe -i PFXFile -c LOCAL_MACHINE\My -a “AccountName” Step 3: Copy the client certificate from the local user store to the local machine storeIf an interactive application such as a Windows Forms application or a command-line application can access the client certificate, the client certificate is already stored in the local user store. However, if a service application such as an ASP.NET Web application cannot access the same client certificate, the client certificate may not be stored in the local machine store.This step explains how to copy a client certificate in the local user store to the local machine store by using the Certificate Export Wizard. Note If the client certificate is already in the local machine store, or if you can install the client certificate directly in the local machine store as in Step 1, go to Step 4. However, if you use Step 3, you must then return to Step 2 to grant access to the client certificate. To copy the client certificate to the local machine store, follow these steps:
Step 4: Install the root certificate of the CAIf the client certificate is already signed by an external CA such as VeriSign, or if you have already installed the root certificate for the CA, you can omit Step 4.By default, Windows has the root certificates of many external CAs already pre-installed in the Trusted Root Certificate Store. Verify that the root certificate is installedTo verify that the root certificate for the CA is installed, follow these steps:
Install the root certificateIf the root certificate of the CA that you want to use is not listed, you must install the root certificate. If the root certificate of the CA that you want to use has been issued to you in a certificate file such as a .cer file, a .der file, or a .pfx file, follow these steps:
Request the root certificateIf the CA that you want to use is included in the Microsoft Certificate Services installation database, you can request the root certificate. To do this, follow these steps:
Call the Web serviceAfter you have installed the client certificate in either the local machine store or the local user store, you can access the client certificate from the ASP.NET Web application to call the Web service. The steps to access the client certificate are the same for a Windows Forms application or for an ASP.NET Web application.If you are using the .NET Framework 1.1, you must first export the key to a DER-encoded file. You must export the key because the System.Security.Cryptography.X509Certificates.X509Certificate class does not contain methods to directly access the details of a certificate from a certificate store. Therefore, the application must read the details of a certificate from a DER-encoded file. Note Web Services Enhancements 2.0 for Microsoft .NET (WSE) provides a way for an application to retrieve the details of a certificate directly from a certificate store. The following C# example code shows how to call a Web service by passing a client certificate for authentication. Web Services Enhancements 2.0 for Microsoft .NETWeb Services Enhancements 2.0 for Microsoft .NET (WSE) is a Microsoft .NET class library for building Web services by using the latest Web services protocols. These protocols include the following:
http://www.microsoft.com/downloads/details.aspx?FamilyId=FC5F06C5-821F-41D3-A4FE-6C7B56423841 (http://www.microsoft.com/downloads/details.aspx?FamilyId=FC5F06C5-821F-41D3-A4FE-6C7B56423841) You do not have to use any one of these protocols to access a Web
service that requires client certificate authentication. However, you may want to use the Microsoft.Web.Services2.Security.X509 class. The Microsoft.Web.Services2.Security.X509 class contains methods to
directly access a client certificate in the certificate store. If you use these methods, you do not have to export the
certificate to a file.The following C# example code shows how to find the first certificate that is named SecureMathClient in the local machine store. Then, this example code uses the certificate to call the Add method of the math Web service. The math Web service requires client certificates. REFERENCESFor more information about the System.Security.Cryptography.X509Certificates.X509Certificate class, visit the following Microsoft Developer Network (MSDN) Web
site: http://msdn2.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate(vs.71).aspx)
For more information about how to call a security-enhanced Web site by using an ASP.NET Web application, click the following article number to view the article in the Microsoft Knowledge Base:
817854 (http://support.microsoft.com/kb/817854/)
FIX: ASP.NET Web application
cannot deliver a client certificate to a security-enhanced Web site
APPLIES TO
| Article Translations
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Back to the top
