首页 文章

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

提问于
浏览
0

我从控制台应用程序调用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 Build 安全通道 . ”}

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

谢谢,

rauts

2 回答

  • 1

    我面临一个非常类似的问题,但在我的情况下,它是另一种方式:当以管理员身份运行一切正常时,在其他情况下检索证书的私钥时出现问题(导致同样的错误:“可能没有为权限XXXX Build SSL / TLS的安全通道 . “) .

    The solution was the following: 在系统上安装证书时,我必须允许导出证书(以及证书的私钥) . 通过这种方式,不仅安装证书的帐户能够使用它进行身份验证,还可以使用所有其他帐户(当然只有访问特定证书存储的用户和存储私钥的文件 . )

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

  • 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
    

相关问题