我的windows 2012服务器机器上有一个本地服务正在运行。它是一个带有一个节点的蔚蓝服务结构集群,但这对此问题并不重要。
该服务运行在“网络服务:用户”下,并具有访问存储在本地计算机上的证书的代码。使用X509Certificate2Collection.Find方法查找证书的代码。
例外是:
引发的异常:“System.AggregateException”在mscorlib.dll中 其他信息:无法使用指纹成功获取访问令牌: 80CEA40A62FFA0116FCB40B7B5985ADCD3E5AC39
当我在本地执行与管理员用户相同的Find函数时,它可以工作。
我已经尝试显式地授予网络服务用户对证书的读取权限,但这并没有解决这个问题。
我还尝试在获得证书的网络服务用户上下文(不是已部署的本地服务,而是本地可执行代码段)下在本地执行代码,这是很奇怪的。
发布于 2017-06-07 12:54:12
我认为问题是网络服务无法访问私钥。在将证书导入到我自己的商店后,我使用下面的代码设置权限,并且我可以访问私钥
Import-PfxCertificate -CertStoreLocation Cert:\LocalMachine\My -FilePath "path to the pfx file"
$certs = Get-ChildItem -Path cert:\LocalMachine\My
Foreach ($cert in $certs)
{
# Specify the user, the permissions and the permission type
$permission = "Network Service","FullControl","Allow"
$accessRule = New-Object -TypeName
System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permission
# Location of the machine related keys
$keyPath = Join-Path -Path $env:ProgramData -ChildPath "\Microsoft\Crypto\RSA\MachineKeys"
$keyName = $cert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
$keyFullPath = Join-Path -Path $keyPath -ChildPath $keyName
# Get the current acl of the private key
$acl = (Get-Item $keyFullPath).GetAccessControl('Access')
# Add the new ace to the acl of the private key
$acl.SetAccessRule($accessRule)
# Write back the new acl
Set-Acl -Path $keyFullPath -AclObject $acl -ErrorAction Stop
# Observe the access rights currently assigned to this certificate.
get-acl $keyFullPath| fl
}https://stackoverflow.com/questions/44334327
复制相似问题