添加一对一映射 <add>

概述

概述 <oneToOneMappings> 元素的 <add> 元素指定单个客户端证书和单个用户帐户之间的唯一映射。

注意

一对一证书映射不同于多对一证书映射,后者可将多个证书映射到单个用户帐户。

兼容性

版本 说明
IIS 10.0 <add> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <add> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <add> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <add> 元素在 IIS 7.5 中未进行修改。
IIS 7.0 IIS 7.0 中引入了 <oneToOneMappings> 元素的 <add> 元素。
IIS 6.0 <oneToOneMappings> 元素取代了 IIS 6.0 IIsCertMapper 元数据库对象。

安装

<iisClientCertificateMappingAuthentication> 元素在 IIS 7 及更高版本的默认安装中不可用。 若要安装它,请使用以下步骤。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在任务栏上,单击 “服务器管理器”。
  2. 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
  3. 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
  4. 在“服务器角色”页上,依次展开“Web 服务器 (IIS)”、“Web 服务器”和“安全性”,然后选择“IIS 客户端证书映射身份验证”。 单击 “下一步”
    Screenshot that shows I I S Client Certificate Mapping Authentication selected for Windows Server 2012.
  5. 在“选择功能”页上,单击“下一步”
  6. “确认安装选择”页上,单击“安装”
  7. 在“结果” 页面中单击“关闭”

Windows 8 或 Windows 8.1

  1. 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 依次展开“Internet Information Services”、“万维网服务”和“安全性”,然后选择“IIS 客户端证书映射身份验证”
    Screenshot that shows I I S Client Certificate Mapping Authentication selected for Windows 8.
  4. 单击“确定”。
  5. 单击“关闭” 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
  2. 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。
  3. 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
  4. 在“添加角色服务向导”的“选择角色服务”页上,选择“IIS 客户端证书映射身份验证”,然后单击“下一步”
    Screenshot that shows I I S Client Certificate Mapping Authentication selected for Windows Server 2008.
  5. “确认安装选择”页中,单击“安装”
  6. 在“结果” 页面中单击“关闭”

Windows Vista 或 Windows 7

  1. 在任务栏上,单击“开始”,然后单击“控制面板”。
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 展开“Internet Information Services”,然后选择“IIS 客户端证书映射身份验证”,然后单击“确定”
    Screenshot that shows I I S Client Certificate Mapping Authentication selected for Windows Vista or Windows 7.

操作方式

IIS 7 中没有用于配置 IIS 客户端证书映射身份验证的用户界面。 有关如何以编程方式配置 IIS 客户端证书映射身份验证的示例,请参阅本文档的代码示例部分。

配置

特性

属性 说明
certificate 必需的字符串属性。

从客户端证书指定 base64 编码的公共证书。 IIS 会将此证书与 Web 客户端通过 Internet 发送的证书副本进行比较。

两个数据字符串必须相同,才能继续映射。 如果客户端获取另一个证书,则即使副本包含与原始证书相同的所有用户信息,它也不会与原始证书匹配。 如果此属性中客户端证书的 base64 编码字符串中有换行符,则必须删除它们。 换行符可能会干扰服务器将服务器的证书与浏览器发送的证书进行比较的能力。
enabled 可选布尔属性。

指定是否启用一对一映射。

默认值为 true
password 可选的字符串属性。

指定用于对客户端进行身份验证的帐户的密码。

注意:为避免在配置文件中存储未加密的密码字符串,请始终使用 AppCmd.exe 输入密码。 如果使用这些管理工具,将密码字符串写入 XML 配置文件之前将对其自动加密。 这提供了相比存储未加密的密码更好的密码安全性。
username 可选的字符串属性。

指定用于验证客户端的帐户的用户名。

子元素

无。

配置示例

以下配置示例启用了使用默认网站的一对一证书映射的 IIS 客户端证书映射身份验证,为用户帐户创建单个一对一证书映射,并将站点配置为要求 SSL 并协商客户端证书。

<location path="Default Web Site">
   <system.webServer>
      <security>
         <access sslFlags="Ssl, SslNegotiateCert" />
         <authentication>
            <windowsAuthentication enabled="false" />
            <anonymousAuthentication enabled="false" />
            <digestAuthentication enabled="false" />
            <basicAuthentication enabled="false" />
            <iisClientCertificateMappingAuthentication enabled="true"
                  oneToOneCertificateMappingsEnabled="true">
               <oneToOneMappings>
                  <add enabled="true"
                     userName="administrator"
                     password="[enc:57686f6120447564652c2049495320526f636b73:enc]"
                     certificate="Base64-Encoded-Certificate-Data" />
               </oneToOneMappings>
            </iisClientCertificateMappingAuthentication>
         </authentication>
      </security>
   </system.webServer>
</location>

如何从客户端证书检索 Base-64 编码字符串

注意

若要检索本主题中所有示例的 Base-64 编码证书数据,可以使用以下步骤导出证书:

  1. 单击“开始”,然后单击“运行”

  2. 键入 MMC,然后单击“确定”

  3. 当 Microsoft 管理控制台打开时,单击“文件”,然后单击“添加/删除管理单元”

  4. 在“添加或删除管理单元”对话框中:

    • 突出显示可用管理单元列表中的“证书”,然后单击“添加”
    • 选择以管理“我的用户帐户”的证书,然后单击“完成”
    • 单击 “确定” 关闭对话框。
  5. 在“Microsoft 管理控制台”中:

    • 依次展开“证书 - 当前用户”、“个人”和“证书”
    • 在证书列表中,右键单击要导出的证书,然后单击“所有任务”和“导出”
  6. 当“证书导出向导”打开时:

    • 单击 “下一步”
    • 选择“不,不导出私钥”,然后单击“下一步”
    • 选择“Base-64 编码的 X.509 9 (.CER)”作为导出格式,然后单击“下一步”
    • 选择将证书保存到桌面并命名为“MyCertificate.cer”,然后单击“下一步”
    • 单击“完成”;应看到一个对话框,提示导出成功。
  7. 关闭 Microsoft 管理控制台。

  8. 使用 Windows 记事本打开导出的 MyCertificate.cer 文件:

    • 删除文本开头的“-----BEGIN CERTIFICATE-----”。
    • 删除文本末尾的“-----END CERTIFICATE-----”。
    • 将所有行连接成一行文本,这是用于本主题中所有示例的 Base-64 编码证书数据。

代码示例

以下代码示例启用了使用默认网站的一对一证书映射的 IIS 客户端证书映射身份验证,为用户帐户创建单个一对一证书映射,并将站点配置为要求 SSL 并协商客户端证书。

注意

若要检索下面列出的代码示例的 Base-64 编码证书数据,可以使用本文档的“配置详细信息”部分中列出的步骤导出证书。

AppCmd.exe

注意

由于 AppCmd.exe 无法分析证书字符串中的某些字符,因此不应使用 AppCmd.exe 来配置 IIS 一对一证书映射。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();

         ConfigurationSection iisClientCertificateMappingAuthenticationSection = config.GetSection("system.webServer/security/authentication/iisClientCertificateMappingAuthentication", "Default Web Site");
         iisClientCertificateMappingAuthenticationSection["enabled"] = true;
         iisClientCertificateMappingAuthenticationSection["oneToOneCertificateMappingsEnabled"] = true;

         ConfigurationElementCollection oneToOneMappingsCollection = iisClientCertificateMappingAuthenticationSection.GetCollection("oneToOneMappings");
         ConfigurationElement addElement = oneToOneMappingsCollection.CreateElement("add");
         addElement["enabled"] = true;
         addElement["userName"] = @"Username";
         addElement["password"] = @"Password";
         addElement["certificate"] = @"Base-64-Encoded-Certificate-Data";
         oneToOneMappingsCollection.Add(addElement);

         ConfigurationSection accessSection = config.GetSection("system.webServer/security/access", "Default Web Site");
         accessSection["sslFlags"] = @"Ssl, SslNegotiateCert";

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration

      Dim iisClientCertificateMappingAuthenticationSection As ConfigurationSection = config.GetSection("system.webServer/security/authentication/iisClientCertificateMappingAuthentication", "Default Web Site")
      iisClientCertificateMappingAuthenticationSection("enabled") = True
      iisClientCertificateMappingAuthenticationSection("oneToOneCertificateMappingsEnabled") = True

      Dim oneToOneMappingsCollection As ConfigurationElementCollection = iisClientCertificateMappingAuthenticationSection.GetCollection("oneToOneMappings")
      Dim addElement As ConfigurationElement = oneToOneMappingsCollection.CreateElement("add")
      addElement("enabled") = True
      addElement("userName") = "Username"
      addElement("password") = "Password"
      addElement("certificate") = "Base-64-Encoded-Certificate-Data"
      oneToOneMappingsCollection.Add(addElement)

      Dim accessSection As ConfigurationSection = config.GetSection("system.webServer/security/access", "Default Web Site")
      accessSection("sslFlags") = "Ssl, SslNegotiateCert"

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var iisClientCertificateMappingAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/iisClientCertificateMappingAuthentication", "MACHINE/WEBROOT/APPHOST/Default Web Site");
iisClientCertificateMappingAuthenticationSection.Properties.Item("enabled").Value = true;
iisClientCertificateMappingAuthenticationSection.Properties.Item("oneToOneCertificateMappingsEnabled").Value = true;

var oneToOneMappingsCollection = iisClientCertificateMappingAuthenticationSection.ChildElements.Item("oneToOneMappings").Collection;
var addElement = oneToOneMappingsCollection.CreateNewElement("add");
addElement.Properties.Item("enabled").Value = true;
addElement.Properties.Item("userName").Value = "Username";
addElement.Properties.Item("password").Value = "Password";
addElement.Properties.Item("certificate").Value = "Base-64-Encoded-Certificate-Data";
oneToOneMappingsCollection.AddElement(addElement);

var accessSection = adminManager.GetAdminSection("system.webServer/security/access", "MACHINE/WEBROOT/APPHOST/Default Web Site");
accessSection.Properties.Item("sslFlags").Value = "Ssl, SslNegotiateCert";

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"

Set iisClientCertificateMappingAuthenticationSection = adminManager.GetAdminSection("system.webServer/security/authentication/iisClientCertificateMappingAuthentication", "MACHINE/WEBROOT/APPHOST/Default Web Site")
iisClientCertificateMappingAuthenticationSection.Properties.Item("enabled").Value = True
iisClientCertificateMappingAuthenticationSection.Properties.Item("oneToOneCertificateMappingsEnabled").Value = True

Set oneToOneMappingsCollection = iisClientCertificateMappingAuthenticationSection.ChildElements.Item("oneToOneMappings").Collection
Set addElement = oneToOneMappingsCollection.CreateNewElement("add")
addElement.Properties.Item("enabled").Value = True
addElement.Properties.Item("userName").Value = "Username"
addElement.Properties.Item("password").Value = "Password"
addElement.Properties.Item("certificate").Value = "Base-64-Encoded-Certificate-Data"
oneToOneMappingsCollection.AddElement(addElement)

Set accessSection = adminManager.GetAdminSection("system.webServer/security/access", "MACHINE/WEBROOT/APPHOST/Default Web Site")
accessSection.Properties.Item("sslFlags").Value = "Ssl, SslNegotiateCert"

adminManager.CommitChanges()