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
} 
4

3 回答 3

2

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-Aclcmdlet 更改文件权限。

您还可以通过证书 MMC 查看权限mmc/add snapin/certificates/computer account/local computer,然后certificates/personal/certificates/[your cert]/all tasks/manage private keys

于 2013-03-19T02:17:36.873 回答
0

如果将证书从 User Store\Personal 拖放到 Computer Store\Personal,则 $getCert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName 值错误。没有具有该值的文件。在正确的存储中再次导入证书,它将起作用。

于 2014-11-14T14:33:07.300 回答
0

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

如何使用 powershell 授予用户对证书私钥的权限?

上面的链接有脚本和示例如何在 powershell 控制台窗口上运行它。

如果您使用的是 ApplicationPoolIdentity,那么您的用户名将是“IIS AppPool\AppPoolNameHere”

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

于 2016-10-14T15:48:57.057 回答