[英]Ignoring invalid SSL certificate
I`m trying to print out log messages from our sub version. 我试图从我们的子版本打印出日志消息。 But I'm struggling with bypassing the invalid SSL certificate.
但我正在努力绕过无效的SSL证书。 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 )
' https:// xxxxx / svn / SiteFabrics / trunk / AppLaunch / Bloc / Frontend '的选项:服务器证书验证失败:为不同的主机名颁发证书,颁发者不受信任( 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. 然而,由于.net错误仍然相同,这没有任何区别。 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;
}
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);
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. 您可以使用像tortoisesvn一样的SVN UI连接到存储库并接受错误的SSL证书然后它将正常工作。 Not a code fix but might work in your instance.
不是代码修复,但可能在您的实例中有效。 It did in mine.
它确实在我的。
It's also possible to use a lambda expression (here in VB) : 也可以使用lambda表达式(这里用VB):
AddHandler client.Authentication.SslServerTrustHandlers, Sub(ssender As Object, ev As SharpSvn.Security.SvnSslServerTrustEventArgs)
ev.AcceptedFailures = ev.Failures
ev.Save = True
End Sub
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
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);
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; };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.