使用 PowerShell 为点到站点连接生成和导出证书Generate and export certificates for Point-to-Site using PowerShell

点到站点连接使用证书进行身份验证。Point-to-Site connections use certificates to authenticate. 本文介绍如何在 Windows 10 或 Windows Server 2016 上使用 PowerShell 创建自签名根证书并生成客户端证书。This article shows you how to create a self-signed root certificate and generate client certificates using PowerShell on Windows 10 or Windows Server 2016. 如果要查找不同的证书说明,请参阅证书 - Linux证书 - MakeCertIf you are looking for different certificate instructions, see Certificates - Linux or Certificates - MakeCert.

必须在运行 Windows 10 或 Windows Server 2016 的计算机上执行本文中的步骤。You must perform the steps in this article on a computer running Windows 10 or Windows Server 2016. 用于生成证书的 PowerShell cmdlet 是操作系统的一部分,在其他版本的 Windows 上不正常工作。The PowerShell cmdlets that you use to generate certificates are part of the operating system and do not work on other versions of Windows. 只需 Windows 10 或 Windows Server 2016 计算机即可生成证书。The Windows 10 or Windows Server 2016 computer is only needed to generate the certificates. 生成证书后,可上传证书,或在任何支持的客户端操作系统上安装该证书。Once the certificates are generated, you can upload them, or install them on any supported client operating system.

如果无法访问 Windows 10 或 Windows Server 2016 计算机,则可使用 Makecert 生成证书。If you do not have access to a Windows 10 or Windows Server 2016 computer, you can use MakeCert to generate certificates. 使用任一方法生成的证书均可安装在支持的所有客户端操作系统上。The certificates that you generate using either method can be installed on any supported client operating system.

创建自签名根证书Create a self-signed root certificate

使用 New-SelfSignedCertificate cmdlet 创建自签名根证书。Use the New-SelfSignedCertificate cmdlet to create a self-signed root certificate. 有关参数的其他信息,请参阅 New-SelfSignedCertificateFor additional parameter information, see New-SelfSignedCertificate.

  1. 在运行 Windows 10 或 Windows Server 2016 的计算机上,使用提升的权限打开 Windows PowerShell 控制台。From a computer running Windows 10 or Windows Server 2016, open a Windows PowerShell console with elevated privileges.

  2. 使用以下示例创建自签名根证书。Use the following example to create the self-signed root certificate. 以下示例创建名为“P2SRootCert”、自动安装在“Certificates-Current User\Personal\Certificates”中的自签名根证书。The following example creates a self-signed root certificate named 'P2SRootCert' that is automatically installed in 'Certificates-Current User\Personal\Certificates'. 打开 certmgr.msc 或“管理用户证书” 即可查看该证书。You can view the certificate by opening certmgr.msc, or Manage User Certificates.

    $cert = New-SelfSignedCertificate -Type Custom -KeySpec Signature `
    -Subject "CN=P2SRootCert" -KeyExportPolicy Exportable `
    -HashAlgorithm sha256 -KeyLength 2048 `
    -CertStoreLocation "Cert:\CurrentUser\My" -KeyUsageProperty Sign -KeyUsage CertSign
    

生成客户端证书Generate a client certificate

在使用点到站点连接连接到 VNet 的每台客户端计算机上,必须安装客户端证书。Each client computer that connects to a VNet using Point-to-Site must have a client certificate installed. 可以从自签名根证书生成客户端证书,导出并安装该客户端证书。You generate a client certificate from the self-signed root certificate, and then export and install the client certificate. 如果不安装客户端证书,身份验证会失败。If the client certificate is not installed, authentication fails.

以下步骤引导完成从自签名根证书生成客户端证书的过程。The following steps walk you through generating a client certificate from a self-signed root certificate. 可以从相同根证书生成多个客户端证书。You may generate multiple client certificates from the same root certificate. 使用以下步骤生成客户端证书时,客户端证书自动安装在用于生成该证书的计算机上。When you generate client certificates using the steps below, the client certificate is automatically installed on the computer that you used to generate the certificate. 如果想要在另一台客户端计算机上安装客户端证书,可以导出该证书。If you want to install a client certificate on another client computer, you can export the certificate.

这些示例使用 New-SelfSignedCertificate cmdlet 生成有效期为一年的客户端证书。The examples use the New-SelfSignedCertificate cmdlet to generate a client certificate that expires in one year. 有关参数的其他信息(例如为客户端证书设置其他有效期),请参阅 New-SelfSignedCertificateFor additional parameter information, such as setting a different expiration value for the client certificate, see New-SelfSignedCertificate.

示例 1Example 1

如果创建自签名根证书之后没有关闭 PowerShell 控制台,请使用此示例。Use this example if you have not closed your PowerShell console after creating the self-signed root certificate. 此示例延续上一节的内容,并使用已声明的“$cert”变量。This example continues from the previous section and uses the declared '$cert' variable. 如果创建自签名根证书后关闭了 PowerShell 控制台,或者要在新的 PowerShell 控制台会话中创建其他客户端证书,请使用示例 2 中的步骤。If you closed the PowerShell console after creating the self-signed root certificate, or are creating additional client certificates in a new PowerShell console session, use the steps in Example 2.

修改并运行示例以生成客户端证书。Modify and run the example to generate a client certificate. 如果在未经修改的情况下直接运行以下示例,会生成名为“P2SChildCert”的客户端证书。If you run the following example without modifying it, the result is a client certificate named 'P2SChildCert'. 如果想要为子证书指定其他名称,请修改 CN 值。If you want to name the child certificate something else, modify the CN value. 运行此示例时,请不要更改 TextExtension。Do not change the TextExtension when running this example. 生成的客户端证书自动安装在计算机上的“Certificates - Current User\Personal\Certificates”中。The client certificate that you generate is automatically installed in 'Certificates - Current User\Personal\Certificates' on your computer.

New-SelfSignedCertificate -Type Custom -DnsName P2SChildCert -KeySpec Signature `
-Subject "CN=P2SChildCert" -KeyExportPolicy Exportable `
-HashAlgorithm sha256 -KeyLength 2048 `
-CertStoreLocation "Cert:\CurrentUser\My" `
-Signer $cert -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2")

示例 2Example 2

若要创建其他客户端证书,或者不想要使用创建自签名根证书时所用的同一个 PowerShell 会话,请使用以下步骤:If you are creating additional client certificates, or are not using the same PowerShell session that you used to create your self-signed root certificate, use the following steps:

  1. 识别安装在计算机上的自签名根证书。Identify the self-signed root certificate that is installed on the computer. 此 cmdlet 返回计算机上安装的证书列表。This cmdlet returns a list of certificates that are installed on your computer.

    Get-ChildItem -Path "Cert:\CurrentUser\My"
    
  2. 从返回的列表中找到使用者名称,并将该名称旁边的指纹复制到文本文件中。Locate the subject name from the returned list, then copy the thumbprint that is located next to it to a text file. 以下示例显示了两个证书。In the following example, there are two certificates. CN 名称是要从中生成子证书的自签名根证书的名称。The CN name is the name of the self-signed root certificate from which you want to generate a child certificate. 在本例中,该根证书为“P2SRootCert”。In this case, 'P2SRootCert'.

    Thumbprint                                Subject
    
    AED812AD883826FF76B4D1D5A77B3C08EFA79F3F  CN=P2SChildCert4
    7181AA8C1B4D34EEDB2F3D3BEC5839F3FE52D655  CN=P2SRootCert
    
  3. 使用上一步骤中获取的指纹为根证书声明变量。Declare a variable for the root certificate using the thumbprint from the previous step. 将 THUMBPRINT 替换为要从中生成子证书的根证书的指纹。Replace THUMBPRINT with the thumbprint of the root certificate from which you want to generate a child certificate.

    $cert = Get-ChildItem -Path "Cert:\CurrentUser\My\THUMBPRINT"
    

    例如,使用上一步骤中 P2SRootCert 的指纹,该变量将如下所示:For example, using the thumbprint for P2SRootCert in the previous step, the variable looks like this:

    $cert = Get-ChildItem -Path "Cert:\CurrentUser\My\7181AA8C1B4D34EEDB2F3D3BEC5839F3FE52D655"
    
  4. 修改并运行示例以生成客户端证书。Modify and run the example to generate a client certificate. 如果在未经修改的情况下直接运行以下示例,会生成名为“P2SChildCert”的客户端证书。If you run the following example without modifying it, the result is a client certificate named 'P2SChildCert'. 如果想要为子证书指定其他名称,请修改 CN 值。If you want to name the child certificate something else, modify the CN value. 运行此示例时,请不要更改 TextExtension。Do not change the TextExtension when running this example. 生成的客户端证书自动安装在计算机上的“Certificates - Current User\Personal\Certificates”中。The client certificate that you generate is automatically installed in 'Certificates - Current User\Personal\Certificates' on your computer.

    New-SelfSignedCertificate -Type Custom -DnsName P2SChildCert -KeySpec Signature `
    -Subject "CN=P2SChildCert" -KeyExportPolicy Exportable `
    -HashAlgorithm sha256 -KeyLength 2048 `
    -CertStoreLocation "Cert:\CurrentUser\My" `
    -Signer $cert -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2")
    

导出根证书公钥 (.cer)Export the root certificate public key (.cer)

创建自签名根证书之后,导出根证书公用密钥 .cer 文件(不导出私钥)。After creating a self-signed root certificate, export the root certificate public key .cer file (not the private key). 稍后要将此文件上传到 Azure。You will later upload this file to Azure. 可借助以下步骤导出自签名根证书的 .cer 文件:The following steps help you export the .cer file for your self-signed root certificate:

  1. 若要获取证书 .cer 文件,请打开“管理用户证书” 。To obtain a .cer file from the certificate, open Manage user certificates. 找到自签名根证书(通常位于“Certificates - Current User\Personal\Certificates”中),并右键单击。Locate the self-signed root certificate, typically in 'Certificates - Current User\Personal\Certificates', and right-click. 单击“所有任务” ,并单击“导出” 。Click All Tasks, and then click Export. 此操作将打开“证书导出向导” 。This opens the Certificate Export Wizard. 如果在 Current User\Personal\Certificates 下找不到证书,可能会意外地打开“Certificates - Local Computer”而不是“Certificates - Current User”)。If you can't find the certificate under Current User\Personal\Certificates, you may have accidentally opened "Certificates - Local Computer", rather than "Certificates - Current User"). 如果想要使用 PowerShell 在当前用户范围内打开证书管理程序,请在控制台窗口中键入“certmgr” 。If you want to open Certificate Manager in current user scope using PowerShell, you type certmgr in the console window.

    导出

  2. 在向导中,单击“下一步” 。In the Wizard, click Next.

    导出证书

  3. 选择“否,不导出私钥” ,并单击“下一步” 。Select No, do not export the private key, and then click Next.

    不要导出私钥

  4. 在“导出文件格式” 页上,选择“Base-64 编码的 X.509 (.CER)” ,并单击“下一步” 。On the Export File Format page, select Base-64 encoded X.509 (.CER)., and then click Next.

    Base-64 编码

  5. 对于“要导出的文件” ,“浏览” 到要将证书导出的目标位置。For File to Export, Browse to the location to which you want to export the certificate. 在“文件名” 中,为证书文件命名。For File name, name the certificate file. 然后单击“下一步”。 Then, click Next.

    浏览

  6. 单击“完成” 导出证书。Click Finish to export the certificate.

    完成

  7. 证书已成功导出。Your certificate is successfully exported.

    Success

  8. 导出的证书类似于以下内容:The exported certificate looks similar to this:

    已导出

  9. 如果使用记事本打开导出的证书,则会看到类似于此示例的一些内容。If you open the exported certificate using Notepad, you see something similar to this example. 蓝色的部分包含上传到 Azure 的信息。The section in blue contains the information that is uploaded to Azure. 如果使用记事本打开证书,并且内容不与此类似,则这通常意味着你没有使用 Base-64 编码的 X.509(.CER) 格式将其导出。If you open your certificate with Notepad and it does not look similar to this, typically this means you did not export it using the Base-64 encoded X.509(.CER) format. 此外,如果希望使用其他文本编辑器,请注意,某些编辑器可能会在后台引入意外的格式设置。Additionally, if you want to use a different text editor, understand that some editors can introduce unintended formatting in the background. 将此证书中的文本上传到 Azure 时,这可能会产生问题。This can create problems when uploaded the text from this certificate to Azure.

    使用记事本打开

导出自签名根证书和私钥以将其存储(可选)Export the self-signed root certificate and private key to store it (optional)

可能需要导出自签名根证书并将它存储在安全位置作为备份。You may want to export the self-signed root certificate and store it safely as backup. 如果需要,可以稍后在另一台计算机上安装此自签名根证书,并生成更多客户端证书。If need be, you can later install it on another computer and generate more client certificates. 如果要将自签名根证书导出为 .pfx,请选择该根证书,并使用导出客户端证书中所述的步骤导出。To export the self-signed root certificate as a .pfx, select the root certificate and use the same steps as described in Export a client certificate.

导出客户端证书Export the client certificate

生成客户端证书时,该证书会自动安装在用于生成它的计算机上。When you generate a client certificate, it's automatically installed on the computer that you used to generate it. 如果想要在另一台客户端计算机上安装客户端证书,需要导出生成的客户端证书。If you want to install the client certificate on another client computer, you need to export the client certificate that you generated.

  1. 若要导出客户端证书,请打开“管理用户证书” 。To export a client certificate, open Manage user certificates. 生成的客户端证书默认位于“Certificates - Current User\Personal\Certificates”中。The client certificates that you generated are, by default, located in 'Certificates - Current User\Personal\Certificates'. 右键单击要导出的客户端证书,单击“所有任务” ,并单击“导出” 打开“证书导出向导” 。Right-click the client certificate that you want to export, click all tasks, and then click Export to open the Certificate Export Wizard.

    导出

  2. 在“证书导出向导”中,单击“下一步”以继续。 In the Certificate Export Wizard, click Next to continue.

    下一步

  3. 选择“是,导出私钥” ,并单击“下一步” 。Select Yes, export the private key, and then click Next.

    导出私钥

  4. 在“导出文件格式” 页上,保留选择默认值。On the Export File Format page, leave the defaults selected. 请务必选中“包括证书路径中的所有证书(如果可能)”。 Make sure that Include all certificates in the certification path if possible is selected. 此外,此设置还会导出成功客户端身份验证所需的根证书信息。This setting additionally exports the root certificate information that is required for successful client authentication. 否则,客户端身份验证会失败,因为客户端没有受信任的根证书。Without it, client authentication fails because the client doesn't have the trusted root certificate. 然后单击“下一步”。 Then, click Next.

    导出文件格式

  5. 在“安全性”页上,必须保护私钥。On the Security page, you must protect the private key. 如果选择使用密码,请务必记下或牢记为此证书设置的密码。If you select to use a password, make sure to record or remember the password that you set for this certificate. 然后单击“下一步”。 Then, click Next.

    security

  6. 在“要导出的文件” 中,“浏览” 到要将证书导出的目标位置。On the File to Export, Browse to the location to which you want to export the certificate. 在“文件名” 中,为证书文件命名。For File name, name the certificate file. 然后单击“下一步”。 Then, click Next.

    要导出的文件

  7. 单击“完成” 导出证书。Click Finish to export the certificate.

    完成

安装已导出的客户端证书Install an exported client certificate

通过 P2S 连接连接到 VNet 的每个客户端都需要在本地安装客户端证书。Each client that connects to the VNet over a P2S connection requires a client certificate to be installed locally.

若要安装客户端证书,请参阅为点到站点连接安装客户端证书To install a client certificate, see Install a client certificate for Point-to-Site connections.

后续步骤Next steps

继续使用点到站点配置。Continue with your Point-to-Site configuration.