2009-06-30 75 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並導入證書。

相關問題