如何使用 SSL 证书配置端口

 

创建使用自承载的 Windows Communication Foundation (WCF) 服务时WSHttpBinding类,使用传输安全,还必须使用 X.509 证书配置端口。 如果不是在创建自承载服务,可以在 Internet 信息服务 (IIS) 上承载服务。 有关详细信息,请参阅HTTP 传输安全性

 

若要配置端口,使用的工具取决于计算机运行的操作系统。

如果运行的是 Windows Server 2003 或 Windows XP,则使用 HttpCfg.exe 工具。 Windows Server 2003 中已安装该工具。 Windows XP,可以下载该工具在Windows XP Service Pack 2 支持工具。 有关详细信息,请参阅Httpcfg 概述。 Windows 支持工具文档说明了 Httpcfg.exe 工具的语法。

如果运行的是 Windows Vista,则使用已安装的 Netsh.exe 工具。

本主题介绍如何完成以下一些过程:

  • 确定计算机当前的端口配置。

  • 获取证书的指纹(以下两个过程需要证书指纹)。

  • 将 SSL 证书绑定到端口配置。

  • 将 SSL 证书绑定到端口配置并支持客户端证书。

  • 从某个端口号删除 SSL 证书。

请注意,修改存储于计算机上的证书需要管理特权。

确定如何配置端口

  1. 在中Windows Server 2003或Windows XP,使用 HttpCfg.exe 工具查看当前端口配置中,使用查询并ssl切换时,如下面的示例中所示。

    httpcfg query ssl  
    
  2. 在 Windows Vista 中,使用 Netsh.exe 工具查看当前端口配置,如下面的示例所示。

    netsh http show sslcert  
    

获取证书的指纹

  1. 使用证书 MMC 管理单元查找用于客户端身份验证的 X.509 证书。 有关详细信息,请参阅如何:使用 MMC 管理单元查看证书

  2. 访问证书的指纹。 有关详细信息,请参阅如何: 检索证书的指纹

  3. 将证书指纹复制到文本编辑器,如 Notepad。

  4. 移除十六进制字符之间的所有空格。 完成此操作的一种方法是使用文本编辑器的“查找和替换”功能,将每个空格替换为空字符。

将 SSL 证书绑定至端口号

  1. 在 Windows Server 2003 或 Windows XP 中,对安全套接字层 (SSL) 存储区使用 HttpCfg.exe 工具的“set”命令将证书绑定至端口号。 该工具使用指纹识别证书,如下面的示例所示。

    httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6  
    
    • -I交换机具有的语法IP:port ,指示该工具将证书设置为计算机的端口 8012。 另外,也可将端口号前面的四个零替换为计算机的实际 IP 地址。

    • -H开关指定证书的指纹。

  2. 在 Windows Vista 中使用 Netsh.exe 工具,如下面的示例所示。

    netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}   
    
    • Certhash参数指定的证书的指纹。

    • Ipport参数指定的 IP 地址和端口,以及功能类似 -i前述 Httpcfg.exe 工具的开关。

    • Appid参数是一个 GUID,用于标识所属应用程序。

将 SSL 证书绑定至端口号并支持客户端证书

  1. 在 Windows Server 2003 或 Windows XP 中,若要支持在传输层使用 X.509 证书进行身份验证的客户端,请按照前面的步骤进行操作,但要向 HttpCfg.exe 另外传递一个命令行参数,如下面的示例所示。

    httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6 -f 2  
    

    -F开关的语法的n其中 n 是介于 1 和 7 之间的数字。 值为 2 可在传输层启用客户端证书,如上面的示例所示。 值为 3 可启用客户端证书并将这些证书映射至 Windows 帐户。请参见“HttpCfg.exe 帮助”以获取其他值的行为。

  2. 在 Windows Vista 中,若要支持在传输层使用 X.509 证书进行身份验证的客户端,请按照前面的步骤进行操作,但要另外提供一个参数,如下面的示例所示。

    netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF} clientcertnegotiation=enable  
    

删除端口号的 SSL 证书

  1. 使用 HttpCfg.exe 或 Netsh.exe 工具查看计算机上的端口和所有绑定的指纹。 若要将信息输出到磁盘,请使用重定向字符“>”,如下面的示例所示。

    httpcfg query ssl>myMachinePorts.txt  
    
  2. 在中Windows Server 2003或Windows XP,使用 HttpCfg.exe 工具以及删除并ssl关键字。使用 -i开关指定IP:port数,并且 -h开关指定指纹。

    httpcfg delete ssl -i 0.0.0.0:8005 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6  
    
  3. 在 Windows Vista 中使用 Netsh.exe 工具,如下面的示例所示。

    Netsh http delete sslcert ipport=0.0.0.0:8005  
    

示例

下面的代码演示如何使用 WSHttpBinding 类(设置为传输安全)创建自承载服务。 创建应用程序时,请指定地址中的端口号。

C#
// This string uses a function to prepend the computer name at run time.
string addressHttp = String.Format(
    "http://{0}:8080/Calculator",
    System.Net.Dns.GetHostEntry("").HostName);

WSHttpBinding b = new WSHttpBinding();
b.Security.Mode = SecurityMode.Transport;
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

// You must create an array of URI objects to have a base address.
Uri a = new Uri(addressHttp);
Uri[] baseAddresses = new Uri[] { a };

// Create the ServiceHost. The service type (Calculator) is not
// shown here.
ServiceHost sh = new ServiceHost(typeof(Calculator), baseAddresses);

// Add an endpoint to the service. Insert the thumbprint of an X.509 
// certificate found on your computer. 
Type c = typeof(ICalculator);
sh.AddServiceEndpoint(c, b, "MyCalculator");
sh.Credentials.ServiceCertificate.SetCertificate(
    StoreLocation.LocalMachine,
    StoreName.My,
    X509FindType.FindBySubjectName,
    "contoso.com");

// This next line is optional. It specifies that the client's certificate
// does not have to be issued by a trusted authority, but can be issued
// by a peer if it is in the Trusted People store. Do not use this setting
// for production code. The default is PeerTrust, which specifies that 
// the certificate must originate from a trusted certifiate authority.

// sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
// X509CertificateValidationMode.PeerOrChainTrust;
try
{
    sh.Open();

    string address = sh.Description.Endpoints[0].ListenUri.AbsoluteUri;
    Console.WriteLine("Listening @ {0}", address);
    Console.WriteLine("Press enter to close the service");
    Console.ReadLine();
    sh.Close();
}
catch (CommunicationException ce)
{
    Console.WriteLine("A commmunication error occurred: {0}", ce.Message);
    Console.WriteLine();
}
catch (System.Exception exc)
{
    Console.WriteLine("An unforseen error occurred: {0}", exc.Message);
    Console.ReadLine();
}

原文 https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate

请参阅

反馈

我们衷心期待你的反馈意见。 请选择要提供的反馈类型:

 

我们就 GitHub 问题建立了新的反馈系统。 阅读我们的博客文章,了解相关内容。

posted @ 2018-12-28 14:59  scoluo  阅读(9434)  评论(0编辑  收藏  举报