How to Grant permission to user on Certificate private key using powershell?

前端 未结 5 1039
感动是毒
感动是毒 2020-12-03 04:45

Certificate is already installed on machine. Now I want to give read permission on PrivateKey of Certificate to application user.

5条回答
  •  無奈伤痛
    2020-12-03 05:08

    The above answer did not work for me as the $_.privatekey returned null. I managed to get access to the private key and assign 'Read' permissions for my Application Pool as follows:

    param (
    [string]$certStorePath  = "Cert:\LocalMachine\My",
    [string]$AppPoolName,
    [string]$certThumbprint
    )
    
    Import-Module WebAdministration
    
    $certificate = Get-ChildItem $certStorePath | Where thumbprint -eq $certThumbprint
    
    if ($certificate -eq $null)
    {
        $message="Certificate with thumbprint:"+$certThumbprint+" does not exist at "+$certStorePath
        Write-Host $message -ForegroundColor Red
        exit 1;
    }else
    {
        $rsaCert = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($certificate)
        $fileName = $rsaCert.key.UniqueName
        $path = "$env:ALLUSERSPROFILE\Microsoft\Crypto\Keys\$fileName"
        $permissions = Get-Acl -Path $path
    
        $access_rule = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS AppPool\$AppPoolName", 'Read', 'None', 'None', 'Allow')
        $permissions.AddAccessRule($access_rule)
        Set-Acl -Path $path -AclObject $permissions
    }
    

提交回复
热议问题