Microsoft KB Archive/315588

= HOW TO: Secure an ASP.NET Application Using Client-Side Certificates =

Article ID: 315588

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft Internet Information Services 5.0
 * Microsoft ASP.NET 1.1
 * Microsoft Internet Information Services 6.0

-



This article was previously published under Q315588



IN THIS TASK
SUMMARY Requirements Create the ASP.NET Web Application Configure the Web Server for Certificate-Based Authentication
 * To Create a Server-Side Certificate Request
 * To Submit a Server-Side Certificate Request
 * To Issue a Server-Side Certificate
 * To Install a Server-Side Certificate
 * To Configure SecureSite Site for SSL and Client-Side Certificates
 * To Verify Certificate-Based Authentication

Install a Client Certificate
 * To Request a Client-Side Certificate
 * To Issue a Client-Side Certificate
 * To Install a Client-Side Certificate

Verify That It Works REFERENCES



SUMMARY
For highly secure Web applications, such as Internet banking sites, you may want to implement a more secure solution for user authentication than the user name and password combination. You can use client-side digital certificates to verify the identity of the user. In addition, you can map client-side digital certificates to Windows accounts on the server if necessary.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 Service Pack 2 Server
 * Microsoft Internet Explorer 6.0
 * Microsoft Visual Studio .NET
 * Microsoft Certificate Services (if you must generate your own certificates)

This article assumes that you are familiar with the following topics:
 * ASP.NET development with Visual Basic or Visual C#
 * Microsoft Internet Information Services (IIS) configuration

back to the top

Create the ASP.NET Web Application
In this section, you create a simple ASP.NET application. In the sections to follow, you will use client-side certificate authentication to secure this application.  Start Visual Studio .NET, and then create a new ASP.NET Web Application project named SecureSite. Drag a Label control from the toolbox onto the WebForm1.aspx Web form, and then set its ID property to greetingLabel . Drag a second Label control onto WebForm1.aspx, and then set its ID property to certDataLabel .  Add the following code to the Page_Load event procedure: 'Visual Basic Dim username As String userName = User.Identity.Name greetingLabel.Text = &quot;Welcome &quot; & userName Dim cert As HttpClientCertificate = Request.ClientCertificate If cert.IsPresent Then 'Get the Organization (O) field from the Subject section. certDataLabel.Text = cert.Get(&quot;Subject O&quot;) Else certDataLabel.Text = &quot;No certificate was found.&quot; End If //Visual C# string userName; userName = User.Identity.Name; greetingLabel.Text = &quot;Welcome &quot; + userName; HttpClientCertificate cert = Request.ClientCertificate; if (cert.IsPresent) certDataLabel.Text = cert.Get(&quot;SUBJECT O&quot;); else certDataLabel.Text=&quot;No certificate was found.&quot;;  Build and save the project, and then close Visual Studio. Start Internet Explorer, and then browse to the following page:

http://localhost/SecureSite/WebForm1.aspx

Notice that the page displays the &quot;Welcome&quot; and &quot;No certificate was found&quot; messages. Notice that the page does not display a user name because the user has not been authenticated. Close Internet Explorer.</li></ol>

back to the top

Configure the Web Server for Certificate-Based Authentication
In this section, you configure the SecureSite project to use certificates for authentication. To use client-side certificates, you must install a server-side certificate. You can use an existing server certificate from any certificate authority, or you can generate a server-side certificate with Microsoft Certificate Services.

back to the top

To Create a Server-Side Certificate Request
<ol> On the Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager.</li> Expand the node for your server, and then click Default Web Site.</li> On the Action menu, click Properties.</li> On the Directory Security tab, click Server Certificate. Follow these steps in the wizard: <ol style="list-style-type: lower-alpha;"> Click Next on the first page of the wizard.</li> On the Server Certificate page, click Create a new certificate, and then click Next.</li> On the Delayed or Immediate Request page, click Prepare the request now, but send it later, and then click Next.</li> On the Name and Security Settings page, accept the default settings, and then click Next.</li> On the Organization Information page, type MSDN for the organization, type How To Articles for the organizational unit, and then click Next.</li> On the Your Site's Common Name page, type localhost, and then click Next.</li> On the Geographical Information page, type your country, region, and city details, and then click Next.</li> On the Certificate Request File Name page, accept the default file name (usually c:\certreq.txt), and then click Next.</li> On the Request File Summary page, confirm that all of the details are correct, and then click Next.</li> Click Finish to close the wizard.</li></ol> </li> Open the certificate file that is generated, and then copy the entire contents of the certificate file to the clipboard.</li></ol>

back to the top

To Submit a Server-Side Certificate Request
<ol> <li>Start Internet Explorer, and then browse to the following page:

http://localhost/CertSrv

NOTE: The Microsoft Certificate Services must be installed.</li> <li>Follow these steps in the wizard: <ol style="list-style-type: lower-alpha;"> <li>Click Request a Certificate, and then click Next.</li> <li>On the Choose Request Type page, click Advanced request, and then click Next.</li> <li>On the Advanced Certificate Requests page, click Submit a certificate request using a base64 encoded PKCS#10 file, and then click Next.</li> <li>On the Submit a Saved Request page, click in the Base64 Encoded Certificate Request (PKCS #10 or #7) box, and then press the CTRL+V key combination to paste the certificate request that you copied to the clipboard earlier. Click Submit.</li></ol> </li> <li>Close Internet Explorer.</li></ol>

back to the top

To Issue a Server-Side Certificate

 * 1) On the Start menu, point to Programs, point to Administrative Tools, and then click Certificate Authority.
 * 2) Expand the node for your certificate authority, and then select Pending Requests.
 * 3) Select the certificate request that you just submitted. On the Action menu, point to All Tasks, and then click Issue.
 * 4) Confirm that the certificate appears in the Issued Certificates folder, and then double-click the certificate to view it.
 * 5) On the Details tab, click Copy to File. Save the certificate as a Base-64 encoded X.509 certificate to C:\Servercert.cer.
 * 6) Close the Properties dialog box for the certificate.
 * 7) Close the Certificate Authority tool.

back to the top

To Install a Server-Side Certificate
<ol> <li>On the Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager.</li> <li>Expand the node for your server, and then click Default Web Site.</li> <li>On the Action menu, click Properties.</li> <li>On the Directory Security tab, click Server Certificate. Follow these steps in the wizard: <ol style="list-style-type: lower-alpha;"> <li>Click Next on the first page of the wizard.</li> <li>Click Process the Pending Request to install the certificate, and then click Next.</li> <li>Browse to the C:\Servercert.cer certificate file that you saved previously. Click Next twice, then click Finish.</li></ol> </li> <li>Click OK to close the Properties dialog box.</li></ol>

back to the top

To Configure SecureSite Site for SSL and Client-Side Certificates
<ol> <li>In Internet Services Manager, select the SecureSite subweb, and view its properties.</li> <li>Follow these steps in the wizard: <ol style="list-style-type: lower-alpha;"> <li>On the Directory Security tab, click Edit in the Secure communications section.</li> <li>Select the Require secure channel (SSL) check box. This ensures that communications to this subweb are encrypted.</li> <li>Select the Require client certificates check box. This ensures that the site can only be viewed by users who have a client-side certificate installed.</li> <li>Select the Enable client certificate mapping check box. This is used to map client certificates to Windows user accounts.</li> <li>Click Edit. Notice that you can map each certificate to an individual Windows account, or you can map many certificates to the same Windows account.</li> <li>On the Many-to-1 tab, click Add to add a mapping rule. Name this rule Mapping Rule, and then click Next.</li> <li>Click New to create a new rule in which the O sub field (Organization) of the Subject section of the certificate has the criteria &quot;MSDN&quot;. Click OK to apply the mapping to users whose certificates contain an Organization attribute of &quot;MSDN&quot;. In a production environment, the mapping rule is stricter and often validates the issuer of the certificate as well as the subject. Click Next.</li> <li>On the Mapping page, select Accept this certificate for Logon Authentication. Click Browse to select the Windows account that you want to map MSDN users to. For this example, use the Administrator account. (In a real application, you create a dedicated Windows account with restricted privileges.) Make sure that you type the correct password.</li> <li>Click Finish, and then confirm the password.</li></ol> </li> <li>Click OK to close the Account Mappings dialog box.</li> <li>Click OK to close the Secure Communications dialog box. If you are prompted to apply the settings to child files and folders, click Select All, and then click OK.</li> <li>Click OK to close the SecureSite Properties dialog box.</li></ol>

back to the top

To Verify Certificate-Based Authentication
<ol> <li>Start Internet Explorer, and then browse to the following page:

https://localhost/SecureSite/WebForm1.aspx

Notice that you use the secure protocol, https.</li> <li>Confirm that you receive a message that the page requires a client certificate.</li> <li>Close Internet Explorer.</li></ol>

back to the top

Install a Client Certificate
In this section, you install a client-side certificate. You can use a certificate from any certificate authority, or you can use Microsoft Certificate Services to generate your own certificate.

back to the top

To Request a Client-Side Certificate
<ol> <li>Start Internet Explorer, and then browse to the following page:

http://localhost/CertSrv

</li> <li>Follow these steps in the wizard: <ol style="list-style-type: lower-alpha;"> <li>Click Request a Certificate, and then click Next.</li> <li>On the Choose Request Type page, click Web Browser Certificate, and then click Next.</li> <li>Type the required information. Make sure that you type MSDN in the Company text box.</li> <li>Click Submit to complete the request.</li></ol> </li> <li>Close Internet Explorer.</li></ol>

back to the top

To Issue a Client-Side Certificate

 * 1) Start the Certificate Authority tool from the Administrative Tools program group.
 * 2) Expand the node for your certificate authority, and then select Pending Requests.
 * 3) Select the certificate request that you just submitted. On the Action menu, point to All Tasks, and then click Issue.
 * 4) Confirm that the certificate appears in the Issued Certificates folder, and then double-click the certificate to view it.
 * 5) On the Details tab, click Copy to File. Save the certificate as a Base-64 encoded X.509 certificate to C:\Clientcert.cer.
 * 6) Close the Properties dialog box for the certificate.
 * 7) Close the Certificate Authority tool.

back to the top

To Install a Client-Side Certificate
<ol> <li>Open Windows Explorer, and double-click Clientcert.cer to view the certificate file.</li> <li>Follow these steps in the Certificate Import Wizard: <ol style="list-style-type: lower-alpha;"> <li>On the first page of the wizard, click Install Certificate, and then click Next.</li> <li>Select the Automatically select the certificate store based on the type of certificate check box, and then click Next.</li> <li>Click Finish to complete the wizard.</li></ol> </li> <li>Dismiss the confirmation message box, and then click OK to close the certificate.</li></ol>

back to the top

Verify That It Works
In this section, you verify that client applications with certificates that specify &quot;MSDN&quot; as the company can view the SecureSite site. Users are mapped to the account that you specified in the mapping rule. <ol> <li>Start Internet Explorer, and then browse to the following page:

https://localhost/SecureSite/WebForm1.aspx

</li> <li>Confirm that: <ul> <li>The Web page appears.</li> <li>The appropriate Windows account is used.</li> <li>The organization &quot;MSDN&quot; is read from the certificate.</li></ul> </li></ol>

back to the top

<div class="references_section">