2012-06-26 87 views
3

我成立了一個FTP服務器IIS與我創造了我自己(使用Makecert.exePvk2Pfx)的SSL證書。我將PFX文件歸因於我的FTP服務器。C#不信任安裝的證書

我有連接到FTP服務器和總是得到以下錯誤消息的C#腳本:

System.Security.Authentication.AuthenticationException:遠程證書根據驗證過程是無效的。

我在本地計算機和用戶的「受信任的根證書頒發機構」中安裝了證書。

,因爲它沒有進行身份驗證,我通過了C#在商店一看:

X509Store store = new X509Store(StoreName.AuthRoot, StoreLocation.LocalMachine); 
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); 

foreach (X509Certificate2 mCert in store.Certificates) 
{ 
    var friendlyName = mCert.Issuer; 
    Console.WriteLine(friendlyName); 
} 
store.Close(); 

但沒有列出我的證書。當我打開MMC console時,我看到了我的證書。

+0

沒有給出什麼理由不相信? – Ben

+0

答案對你有幫助嗎?如果是這樣,請接受答案,如果沒有添加信息澄清。 – Sascha

+0

我很抱歉Sascha,但我無法立即回答您。我忙於另一個重點項目。我會盡快澄清。感謝您的幫助。 – Jeppen

回答

2

通常,C#不信任沒有可信根證書的證書 - 就像自簽名證書一樣。 ServicePointManager允許您添加一個函數,您可以在其中處理信任。

// Callback used to validate the certificate in an SSL conversation 
private static bool ValidateRemoteCertificate(
    object sender, 
    X509Certificate certificate, 
    X509Chain chain, 
    SslPolicyErrors policyErrors) 
{ 
    if (Convert.ToBoolean(ConfigurationManager.AppSettings["IgnoreSslErrors"])) 
    { 
     // Allow any old dodgy certificate... 
     return true; 
    } 
    else 
    { 
     return policyErrors == SslPolicyErrors.None; 
    } 
} 

private static string MakeRequest(string uri, string method, WebProxy proxy) 
{ 
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); 
    webRequest.AllowAutoRedirect = true; 
    webRequest.Method = method; 

    // Allows for validation of SSL conversations 
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(
    ValidateRemoteCertificate); 

    if (proxy != null) 
    { 
     webRequest.Proxy = proxy; 
    } 

    HttpWebResponse response = null; 
    try 
    { 
     response = (HttpWebResponse)webRequest.GetResponse(); 
     using (Stream s = response.GetResponseStream()) 
     { 
      using (StreamReader sr = new StreamReader(s)) 
      { 
       return sr.ReadToEnd(); 
      } 
     } 
    } 
    finally 
    { 
     if (response != null) 
      response.Close(); 
    } 
} 

從博客文章How to accept an invalid SSL certificate programmatically

1

作爲一個快速的解決方法,你可以接受所有的證書:

ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;