使用PowerShell授予IIS 7.5应用程序池读取证书私钥的权限

5
我四处搜索,但没有找到太多信息,基本上我有Windows 2008 R2,我创建了一个PowerShell脚本来将PFX文件加载到本地计算机的证书存储中。
现在我需要使用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
} 
3个回答

2

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

通过PowerShell,您可以在此处查看可用于IIS的证书:

cert:\LocalMachine\My

您可以进入该位置并查找您的证书。一旦找到,您可以使用以下命令查看其私钥 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


请查看我的更新帖子,并检查 Get-LocalMachineCertificate 函数。 - hardywang
不,我的意思是发布代码将PFX中的密钥对安装到证书存储中。 私有密钥文件在我2008 R2虚拟机上的那个文件夹中。 - Andy Arismendi
确定它要存储在机器存储中而不是用户存储中吗?C:\Users\[NAME]\AppData\Roaming\Microsoft\Crypto\RSA\[SID] - Andy Arismendi
是的,你说得对,我在漫游文件夹中找到了它。但是我可以在我的MMC中看到本地计算机存储中的证书,这是怎么回事? - hardywang
请确保您的脚本将其安装到 cert:\LocalMachine\My - Andy Arismendi

0
如果您将用户存储\个人证书拖放到计算机存储\个人证书中,则 $getCert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName 值是错误的。没有该值的文件。请在正确的存储区域重新导入证书,它就可以正常工作了。

0
这是一个完整的PowerShell脚本,用于授予任何用户证书私钥的权限。

如何使用PowerShell为证书私钥上的用户授予权限?

上述链接提供了脚本和示例,说明如何在PowerShell控制台窗口中运行它。

如果您正在使用ApplicationPoolIdentity,则您的用户名将为'IIS AppPool\AppPoolNameHere'

注意:由于IIS和AppPool之间有一个空格,因此您需要使用' '


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接