在管理员模式下运行进程时出现X509证书私钥问题

时间:2012-02-15 17:25:58

标签: .net wcf x509certificate

我正在从控制台应用程序调用一个需要客户端证书进行身份验证的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建立安全通道。”}

的InnerException: {“请求已中止:无法创建SSL / TLS安全通道。”}

感谢,

rauts

2 个答案:

答案 0 :(得分:1)

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

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

有关授予私钥文件here访问权限的详细信息。

答案 1 :(得分:0)

感谢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