【问题标题】:X509Certificate private key issue when running a process in Adminstrator mode在管理员模式下运行进程时 X 509 证书私钥问题
【发布时间】:2012-02-15 17:25:57
【问题描述】:

我正在从控制台应用程序调用需要客户端证书进行身份验证的 WCF 服务。当我在以管理员身份运行的 Visual Studio 2010 中以调试模式运行控制台应用程序时,该应用程序无法将安装在我机器上的 X509 证书作为客户端证书提供,但是当同一程序在 Visual Studio 中运行时(不以管理员身份运行) ,应用程序运行良好,我可以将客户端证书提供给 WCF 服务,WCF 服务也会返回数据。

客户端和服务器证书均由我公司的内部 CA 颁发。我在 Windows 7 上运行,我使用的是 .Net 4.0。

当我有一个 Visual Studio 加载项使用 Mutual SSL 调用相同的 WCF 服务时,我遇到了同样的问题。当我的 VS 在管理员模式下运行时,WCF 服务调用会失败,否则它可以正常工作。

当我在任务管理器中查看 VS 进程时,在这两种情况下(管理员和非管理员),它都将进程用户显示为我的 ID,所以我并不感到困惑,因为这不可能是任何证书访问问题。

任何提示或帮助都会非常有帮助。 代码片段:

 private static void MutualSslServiceCall()
    {

        var testClient = new LocalService.Service1Client("MutualSsl");
        testClient.ClientCredentials.ClientCertificate.Certificate = GetClientCertificate();

        var response = testClient.GetData(3232);
        Console.WriteLine("Done, Resposne = {0}", response);
        Console.ReadLine();
    }


    // gets the certificate from the workstation certificate store.
    private static X509Certificate2 GetClientCertificate()
    {
        X509Certificate2 certificate = null;
        var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        try
        {
            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly| );
            // Nothing to do if no cert found.
            if (store.Certificates != null && store.Certificates.Count > 0)
            {
                if (store.Certificates.Count == 1)
                {
                    // Return the certificate present.
                    certificate = store.Certificates[0];
                }
                else
                {
                    // Request the user to select a certificate
                    var certificates = X509Certificate2UI.SelectFromCollection(store.Certificates, "Digital Certificates", "Select a certificate from the following list:", X509SelectionFlag.SingleSelection);
                    // Check if one has been returned
                    if (certificates != null && certificates.Count > 0)
                    {
                        
                        certificate = certificates[0];
                        
                        
                        
                    }
                }
            }
        }
        finally
        {
            store.Close();
        }
        return certificate;
    }

错误: {“无法为具有 XXXX 权限的 SSL/TLS 建立安全通道。”}

内部异常: {“请求中止:无法创建 SSL/TLS 安全通道。”}

【问题讨论】:

  • 您介意分享分配证书的代码 sn-p 以及任何错误消息吗?
  • 嗨..你至少能够加载证书吗?我的意思是证书=证书[0];里面有证书吗?
  • 是的...我可以在证书集合中加载证书,但是当我尝试使用它时,它需要私钥,这就是它失败的时候

标签: .net wcf x509certificate


【解决方案1】:

我遇到了一个非常相似的问题,但在我的情况下,情况正好相反:以管理员身份运行时一切正常,在其他情况下,检索证书的私钥时出现问题(导致相同的错误:“无法为具有 XXXX 权限的 SSL/TLS 建立安全通道。”)。

解决方案如下: 在系统上安装证书时,我必须允许导出证书(以及证书的私钥)。这样,不仅安装证书的帐户可以使用它进行身份验证,所有其他人也可以使用它(当然只有那些有权访问特定证书存储和存储私钥的文件的帐户。)

有关授予对私钥文件 here 的访问权限的更多信息。

【讨论】:

    【解决方案2】:

    感谢qqbenq指出关键问题。正如qqbenq建议的那样,您的帐户没有访问证书私钥的权限。

    这是 Powershell 上的解决方案:

    $thumbprint = 'Your_Cert_Thumprint'
    $WorkingCert = Get-ChildItem CERT:\LocalMachine\My\$thumbprint
    $rsaFile = $WorkingCert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
    
    $keyPath = "C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys"
    $rsaPath = join-path $keyPath $rsaFile
    $acl = Get-Acl -Path $rsaPath
    $permission = "Authenticated Users","Read","Allow"
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
    $acl.AddAccessRule($accessRule)
    Set-Acl $rsaPath $acl
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 2020-04-10
      • 2019-07-02
      • 2018-03-03
      • 2020-01-25
      相关资源
      最近更新 更多