【问题标题】:NETWORK SERVICE user cannot read certificate windows server 2012网络服务用户无法读取证书 windows server 2012
【发布时间】:2017-11-04 04:24:36
【问题描述】:

我在我的 Windows 2012 服务器机器上运行了一个本地服务。它是一个带有一个节点的 Azure Service Fabric 集群,但这对于这个问题应该无关紧要。

该服务在“NETWORK SERVICE: 用户下运行,并且具有访问存储在本地计算机上的证书的代码。使用 X509Certificate2Collection.Find 方法查找证书的代码。

例外是:

抛出异常:mscorlib.dll 中的“System.AggregateException”

附加信息:无法成功获取访问令牌 使用指纹:80CEA40A62FFA0116FCB40B7B5985ADCD3E5AC39

当我在本地执行与管理员用户相同的参数的相同查找功能时,它可以工作。

我已尝试明确授予 NETWORK SERVICE 用户对证书的读取权限,但这并没有解决问题。

我也尝试过在 NETWORK SERVICE 用户上下文(不是部署的本地服务,而是本地可执行代码 sn-p)下本地执行代码,获得证书,这很有效。

【问题讨论】:

    标签: c# windows web-services x509certificate azure-service-fabric


    【解决方案1】:

    我认为问题在于网络服务无法访问私钥。在将证书导入我自己的商店并访问私钥后,我使用以下代码设置权限

    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
     }
    

    【讨论】:

    • 我已经通过 Windows MMC 证书管理器授予了私钥的权限(这就是我所说的“授予用户对证书的读取权限”的意思),但我会尝试脚本
    • 关于如何找到与我的证书关联的 .pfx 文件有任何线索吗?
    • 如果您没有原件,请以有权访问私钥的用户身份登录,然后将其与私钥一起导出到 pfx :)
    猜你喜欢
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 2018-04-19
    相关资源
    最近更新 更多