2011-11-14 200 views
11

我試圖從我們的子版本打印日誌消息。但我正在繞過無效的SSL證書而掙扎。這是錯誤:忽略無效SSL證書

OPTIONS of ' https://xxxxx/svn/SiteFabrics/trunk/AppLaunch/Bloc/Frontend ': Server certificate verification failed: certificate issued for a different hostname, issuer is not trusted (https://xxxx)

我忽略證書錯誤的嘗試是加入這一行:

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; 

但是這並沒有任何區別的.NET錯誤仍然是相同的。下面是代碼,任何人都可以看到我做錯了什麼?

 using (SvnClient client = new SvnClient()) 
     { 
      Collection<SvnLogEventArgs> list; 
      client.Authentication.DefaultCredentials = new NetworkCredential("user", "pass"); 

      ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; 

      SvnLogArgs la = new SvnLogArgs(); //{ Start=128; End=132; }; 
      client.LoadConfiguration(Path.Combine(Path.GetTempPath(), "Svn"), true); 
      client.GetLog(new Uri("https://[svnurl]"), la, out list); 
      ViewBag.SVNLog = list; 
     } 
+0

您是否看過這篇文章?:http://stackoverflow.com/questions/3099392/svn-repository-authentication-using-sharpsvn – Tomas

+0

在最近的SharpSvn版本中,您可以使用.UseDefaultConfiguration()代替.LoadConfiguration避免使用臨時目錄。 –

回答

3

發現這個問題的辦法:

首先補充一點:

 static void SVN_SSL_Override(object sender, SharpSvn.Security.SvnSslServerTrustEventArgs e) 
    { 
     e.AcceptedFailures = e.Failures; 
     e.Save = true; 
    } 

然後替換我原來的魔線:

  ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; 

與此:

client.Authentication.SslServerTrustHandlers += new EventHandler<SharpSvn.Security.SvnSslServerTrustEventArgs>(SVN_SSL_Override); 
0

您可以連接到使用SVN UI像tortoisesvn一個單一的時間資源庫並接受不良SSL證書,然後它會正常工作。不是代碼修復,但可能適用於您的實例。它在我的。

+0

對,將證書頒發者添加到您的可信證書頒發機構列表中,這比接受任何證書更安全明智。 – Paciv

+1

這正是.SslServerTrustHandlers實現通過設置.Save = true所做的。但是,自己保存可能不起作用,因爲調用.LoadConfiguration()時不會使用默認配置位置。 –

0

它也可以使用(在這裏VB)lambda表達式:

AddHandler client.Authentication.SslServerTrustHandlers, Sub(ssender As Object, ev As SharpSvn.Security.SvnSslServerTrustEventArgs) 
    ev.AcceptedFailures = ev.Failures 
    ev.Save = True 
End Sub 
0
private void GetClaimParams(string targetUrl, out string loginUrl, out Uri navigationEndUrl) 
     { 
HttpWebRequest webRequest = null; 
      WebResponse response = null; 
      webRequest = (HttpWebRequest)WebRequest.Create(targetUrl); 
      webRequest.Method = Constants.WR_METHOD_OPTIONS; 
      #if DEBUG 
       ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(IgnoreCertificateErrorHandler); 
      #endif 
      try 
      { 
       response = (WebResponse)webRequest.GetResponse(); 
       ExtraHeadersFromResponse(response, out loginUrl, out navigationEndUrl); 
      } 
      catch (WebException webEx) 
      { 
       ExtraHeadersFromResponse(webEx.Response, out loginUrl, out navigationEndUrl); 
      } 
} 



#if DEBUG 
     private bool IgnoreCertificateErrorHandler 
      (object sender, 
      System.Security.Cryptography.X509Certificates.X509Certificate certificate, 
      System.Security.Cryptography.X509Certificates.X509Chain chain, 
      System.Net.Security.SslPolicyErrors sslPolicyErrors) 
     { 
      return true; 
     } 
#endif // DEBUG 
0

我用這一個...只是試試看:

//As given in the url to handle invalid SSL : http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.servercertificatevalidationcallback.aspx 

       ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertificatePolicy.CheckValidationResult); 
0

漂亮與上述答案非常相似,只是將回調作爲代理傳遞。你可以給它一個嘗試,可能爲你工作 -

ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };