2009-06-30 79 views
1

我已经发布了PKCS#12证书,用于访问简单的基于xml的Web服务。当我将PKCS#12文件加载到Windows(Vista)中时,我可以使用浏览器访问该服务。如何在.NET WebRequest中使用PKCS#12证书文件?

试图通过应用程序来访问服务,而无需加载PKCS#12到OS证书收藏,我写了下面的代码:

// The certificate i'm using can not be authenticated as it is a development one. 
// For now, just ignore it. 
static bool myRemoteCertificateValidationCallback(
     Object sender, 
     X509Certificate certificate, 
     X509Chain chain, 
     SslPolicyErrors sslPolicyErrors 
) 
{ return true; } 

static void Main(string[] args) 
{ 
    ServicePointManager.ServerCertificateValidationCallback = myRemoteCertificateValidationCallback; 
    X509Certificate Cert = new X509Certificate(@"certificatefile.p12","medialab"); 
    HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("https://ServiceURL"); 
    Req.ClientCertificates.Add(Cert); 

    Stream S = Req.GetResponse().GetResponseStream(); 
    TextReader TR = new StreamReader(S); 
    string Ret = TR.ReadToEnd(); 
    Console.Write(Ret); 

} 

可悲的是这个代码失败,我得到一个System.Net .WebException:请求已中止:无法创建SSL/TLS安全通道。我注意到,当我将PKCS#12文件加载到Windows时,代码突然生效。

我需要做些什么才能独立完成文件并避免使用Windows证书存储?

感谢, 波阿斯

更多信息:刚刚申请SP1到我的Visual Studio和现在我得到一个不同的异常:“一个调用SSPI失败,请参见内部异常”与内部异常 - >“的收到的消息意外或格式不正确。“

回答

2

您必须将证书安装在证书存储区中。最简单的方法是使用IE并导入证书。

相关问题