簡體   English   中英

使用PowerShell授予IIS 7.5應用程序池讀取證書私鑰的權限

[英]Grant IIS 7.5 Application Pool Read Permission to Certificate Private Key by Using PowerShell

我四處搜索,找不到很多信息,基本上我有Windows 2008 R2,我創建了PowerShell腳本,將PFX文件加載到Local Machine的證書存儲區。

現在,我需要授予應用程序池的權限,以便使用PowerShell讀取證書的私鑰。

在舊的Windows 2003方式中,我只需要將實際文件放在C:\\ProgramData\\Microsoft\\Crypto\\RSA\\MachineKeys\\文件夾中,但看起來Win 2008使用的是另一個文件夾。

有人有解決方案嗎?

- 更新我的代碼版本 -

function Grant-CertificatePermissions([string]$certSubject,[string]$user,[string]$permissionType,[string]$permission = $args[3])
{
    $getCert = Get-LocalMachineCertificate $certSubject
    $keypath = Get-CertificateStorePath
    $certHash = $getCert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
    $certFullPath = $keypath+$certHash
    $certAcl = Get-Acl -Path $certFullPath

    try
    {
        $accessRule=new-object System.Security.AccessControl.FileSystemAccessRule $user, $permissionType, $permission
        $certAcl.AddAccessRule($accessRule)
    }
    catch [System.Exception]
    {
        throw "Invalid User Id Or Permission"
    }
    Set-Acl $certFullPath $certAcl
}

function Get-LocalMachineCertificate([string]$subject, [string]$certificateStoreLocation, [string]$certificateStoreName)
{
    $getCert = Get-ChildItem -Recurse Cert:\$certificateStoreLocation\$certificateStoreName | Where-Object {$_.Subject -eq $subject}

    if(!$getCert)
    {
        throw "Certificate Not Found"
    }

    return $getCert
}

function Get-CertificateStorePath
{
    $commonCertPathStub = "\Microsoft\Crypto\RSA\MachineKeys\"
    $programData = $Env:ProgramData
    if(!$programData)
    {
        $programData = $Env:ALLUSERSPROFILE + "\Application Data"
    }

    $keypath = $programData + $commonCertPathStub

    return $keypath
}

在我的Get-CertificateStorePath函數中,我得到的值是C:\\ProgramData\\Microsoft\\Crypto\\RSA\\MachineKeys\\ ,在我獲得證書哈希之后,完整的文件看起來像C:\\ProgramData\\Microsoft\\Crypto\\RSA\\MachineKeys\\d82829f7770ea5d85ef978dea67f302d_4cca7190-7e9f-46d7-b180-6656fec432e2 ,當我執行Get-Acl行時我有異常Cannot find path 'C:\\ProgramData\\Microsoft\\Crypto\\RSA\\MachineKeys\\d82829f7770ea5d85ef978dea67f302d_4cca7190-7e9f-46d7-b180-6656fec432e2' because it does not exist.

我瀏覽了那個文件夾,我確實找不到這樣的文件。

- 更新 -

function Import-PfxCertificate ([String]$certPath,[String]$certificateStoreLocation ,[String]$certificateStoreName, $pfxPassword)
{
    $pfx = new-object System.Security.Cryptography.X509Certificates.X509Certificate2    

    $pfx.Import($certPath, $pfxPassword, "Exportable,PersistKeySet")    

    $store = new-object System.Security.Cryptography.X509Certificates.X509Store($certificateStoreName,$certificateStoreLocation)    
    $store.open("MaxAllowed")    
    $store.add($pfx)    
    $store.close()
    return $pfx
} 

2008 R2使用C:\\ProgramData\\Microsoft\\Crypto\\RSA\\MachineKeys

通過PowerShell,您可以在此處查看IIS可用的證書:

cert:\LocalMachine\My

您可以cd到該位置並查找您的證書。 找到后,您可以使用以下方法查看其私鑰ID:

$cert = get-item 2779B37AE3625FD8D2F9596E285C7CDC15049D87
$cert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName

這將包含MachineKeys文件夾中的長十六進制文件名。

然后,您可以使用Set-Acl cmdlet更改文件權限。

您還可以通過證書MMC mmc/add snapin/certificates/computer account/local computer查看權限,然后certificates/personal/certificates/[your cert]/all tasks/manage private keys

如果將證書從User Store \\ Personal拖放到Computer Store \\ Personal,則$ getCert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName值是錯誤的。 沒有包含該值的文件。 在正確的商店中再次導入證書,它將工作。

這是一個完整的PowerShell腳本,可以為證書私鑰上的任何用戶授予權限。

如何使用PowerShell為證書私鑰授予用戶權限?

上面的鏈接有腳本和示例如何在powershell控制台窗口上運行它。

如果您使用的是ApplicationPoolIdentity,那么您的用戶名將是'IIS AppPool \\ AppPoolNameHere'

注意 :您需要使用'',因為IIS和AppPool之間有空格。

暫無
暫無

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

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