簡體   English   中英

如何從命令提示符向 IIS7 站點分配 SSL 證書

[英]How to assign a SSL Certificate to IIS7 Site from Command Prompt

您能否告訴我是否可以使用 APPCMD 應用程序為 IIS7 中的網站分配 SSL 證書?

我熟悉設置HTTPS綁定的命令

appcmd set site /site.name:"A Site" /+bindings.[protocol='https',bindingInformation='*:443:www.mysite.com']

以及如何獲取當前映射

%windir%\system32\inetsrv\Appcmd

但似乎找不到任何方法將站點映射到證書(例如證書哈希)

答案是使用NETSH。 例如

netsh http add sslcert ipport=0.0.0.0:443 certhash='baf9926b466e8565217b5e6287c97973dcd54874' appid='{ab3c58f7-8316-42e3-bc6e-771d4ce4b201}'

這對我有很大幫助:Sukesh Ashok Kumar 的一個簡單指南,用於從命令行為 IIS 設置 SSL。 包括使用certutil / makecert導入/生成證書。

http://www.awesomeideas.net/post/How-to-configure-SSL-on-IIS7-under-Windows-2008-Server-Core.aspx

編輯:如果原始 URL 已關閉,它仍然可以通過 Wayback Machine 獲得

使用 PowerShell 和 WebAdministration 模塊,您可以執行以下操作以將 SSL 證書分配給 IIS 站點:

# ensure you have the IIS module imported
Import-Module WebAdministration

cd IIS:\SslBindings
Get-Item cert:\LocalMachine\My\7ABF581E134280162AFFFC81E62011787B3B19B5 | New-Item 0.0.0.0!443

注意事項...值“7ABF581E134280162AFFFC81E62011787B3B19B5”是您要導入的證書的指紋。 所以需要先導入到證書庫中。 New-Item cmdlet 接收 IP 地址(所有 IP 均為 0.0.0.0)和端口。

有關更多詳細信息,請參閱http://learn.iis.net/page.aspx/491/powershell-snap-in-configuring-ssl-with-the-iis-powershell-snap-in/

我已經在 Windows Server 2008 R2 以及 Windows Server 2012 預發行版中對此進行了測試。

@David 和 @orip 說得對。

但是,我確實想提及示例中指定的ipport參數 (0.0.0.0:443) 是 MSDN 所稱的“未指定地址(IPv4:0.0.0.0 或 IPv6:[::])”。

我去查了一下,所以我想我會在這里記錄以節省其他人的時間。 本文重點介紹 SQL Server,但信息仍然相關:

http://msdn.microsoft.com/en-us/library/ms186362.aspx

使用這篇文章中的答案,我創建了一個腳本來為我解決問題。 它從 pfx 文件開始,但您可以跳過該步驟。

這里是:

cd C:\Windows\System32\inetsrv

certutil -f -p "pa$$word" -importpfx "C:\temp\mycert.pfx"

REM The thumbprint is gained by installing the certificate, going to cert manager > personal, clicking on it, then getting the Thumbprint.
REM Be careful copying the thumbprint. It can add hidden characters, esp at the front.
REM appid can be any valid guid
netsh http add sslcert ipport=0.0.0.0:443 certhash=5de934dc39cme0234098234098dd111111111115 appid={75B2A5EC-5FD8-4B89-A29F-E5D038D5E289}

REM bind to all ip's with no domain. There are plenty of examples with domain binding on the web
appcmd set site "Default Web Site" /+bindings.[protocol='https',bindingInformation='*:443:']

如果您嘗試在不使用 MMC 管理單元 GUI 的情況下執行 IIS 管理,則應使用 powershell WebAdministration 模塊。

此博客上的其他答案不適用於更高版本的 Windows Server (2012)

使用PowerShell + netsh

$certificateName = 'example.com'
$thumbprint = Get-ChildItem -path cert:\LocalMachine\My | where { $_.Subject.StartsWith("CN=$certificateName") } | Select-Object -Expand Thumbprint
$guid = [guid]::NewGuid().ToString("B")
netsh http add sslcert ipport="0.0.0.0:443" certhash=$thumbprint certstorename=MY appid="$guid"

如果您需要命名綁定,請將netsh調用替換為:

netsh http add sslcert hostnameport="$certificateName:443" certhash=$thumbprint certstorename=MY appid="$guid"

使用 IISAdministration 1.1.0.0 ( https://www.powershellgallery.com/packages/IISAdministration/1.1.0.0 ),您可以使用以下代碼將新的 HTTPS 綁定添加到特定站點:

$thumbPrint = (gci Cert:\localmachine\My | Where-Object { $_.Subject -Like "certSubject*" }).Thumbprint
New-IISSiteBinding -Name "Site Name" -BindingInformation "*:443:" -CertificateThumbPrint $thumbPrint -CertStoreLocation My -Protocol https

查看現有綁定

Get-IISSiteBinding -Name "Site Name"

刪除現有綁定

Remove-IISSiteBinding -Name "Site Name" -BindingInformation "*:443:" -Protocol https -Confirm:$False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM