【发布时间】: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