11

I`m trying to print out log messages from our sub version. But I'm struggling with bypassing the invalid SSL certificate. This is the error:

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)

My attempt of ignoring the certificate error was to add this line:

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

However that didn't make any difference as the .net error is still the same. Below is the code, can anyone see what I am doing wrong?

        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;
        }
Undo
  • 25,519
  • 37
  • 106
  • 129
Joakim
  • 1,979
  • 2
  • 16
  • 25
  • Have you looked at this post?: http://stackoverflow.com/questions/3099392/svn-repository-authentication-using-sharpsvn – Tomas Nov 14 '11 at 12:18
  • In recent SharpSvn versions you can use .UseDefaultConfiguration() instead of .LoadConfiguration to avoid using a temp dir. – Bert Huijben Sep 07 '12 at 11:03

6 Answers6

3

FOUND THE SOLUTION TO THIS PROBLEM:

First add this:

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

and then replace my original magic line:

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

with this:

client.Authentication.SslServerTrustHandlers += new EventHandler<SharpSvn.Security.SvnSslServerTrustEventArgs>(SVN_SSL_Override);
Joakim
  • 1,979
  • 2
  • 16
  • 25
0

You could connect to the repository using a SVN UI like tortoisesvn a single time and accept the bad SSL Cert and then it will work fine. Not a code fix but might work in your instance. It did in mine.

Micah Armantrout
  • 6,781
  • 4
  • 40
  • 66
  • Right, add the certificate issuer to your list of trusted certification authorities, which is security wise way better than accepting any certificate. – Paciv Aug 24 '12 at 15:12
  • 1
    This is exactly what the .SslServerTrustHandlers implementation does by setting .Save = true. But saving by itself probably won't work because the default configuration location isn't used when .LoadConfiguration() is called. – Bert Huijben Sep 07 '12 at 10:59
0

It's also possible to use a lambda expression (here in VB) :

AddHandler client.Authentication.SslServerTrustHandlers, Sub(ssender As Object, ev As SharpSvn.Security.SvnSslServerTrustEventArgs)
  ev.AcceptedFailures = ev.Failures
  ev.Save = True
End Sub
Pierre
  • 321
  • 2
  • 4
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
Sharique Ansari
  • 1,458
  • 1
  • 12
  • 22
0

I am using this one... just have it a try :

//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);
Dhanasekar Murugesan
  • 3,141
  • 1
  • 18
  • 21
0

Pretty much the same as the above answers, just passing the callback as a delegate. You can give it a try, might work for you -

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