2011-11-14 204 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; };