Jak zignorować sprawdzenie certyfikatu przy ssl
Próbuję znaleźć sposób, aby zignorować sprawdzanie certyfikatu, gdy żądasz zasobu Https, do tej pory znalazłem pomocny artykuł w Internecie.
Ale nadal mam jakiś problem. Proszę przejrzeć mój kod. Po prostu nie rozumiem, co oznacza kodServicePointManager.ServerCertificateValidationCallback
.
Kiedy ta metoda delegata zostanie wywołana? I jeszcze jedno pytanie, w którym miejscu powinienem napisać ten kod? Przed ServicePointManager.ServerCertificateValidationCallback
wykonaniem czy przed Stream stream = request.GetRequestStream()
?
public HttpWebRequest GetRequest()
{
CookieContainer cookieContainer = new CookieContainer();
// Create a request to the server
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_remoteUrl);
#region Set request parameters
request.Method = _context.Request.HttpMethod;
request.UserAgent = _context.Request.UserAgent;
request.KeepAlive = true;
request.CookieContainer = cookieContainer;
request.PreAuthenticate = true;
request.AllowAutoRedirect = false;
#endregion
// For POST, write the post data extracted from the incoming request
if (request.Method == "POST")
{
Stream clientStream = _context.Request.InputStream;
request.ContentType = _context.Request.ContentType;
request.ContentLength = clientStream.Length;
ServicePointManager.ServerCertificateValidationCallback = delegate(
Object obj, X509Certificate certificate, X509Chain chain,
SslPolicyErrors errors)
{
return (true);
};
Stream stream = request.GetRequestStream();
....
}
....
return request;
}
}
10 answers
Ponieważ istnieje tylko jeden globalny ServicePointManager, Ustawienie ServicePointManager.ServerCertificateValidationCallback spowoduje, że wszystkie kolejne żądania odziedziczą tę politykę. Ponieważ jest to globalne "ustawienie", lepiej byłoby ustawić je w metodzie Application_Start w metodzie Global.asax .
Ustawienie wywołania zwrotnego nadpisuje domyślne zachowanie i możesz samodzielnie utworzyć niestandardową procedurę walidacji.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-20 06:26:31
Dla wszystkich zainteresowanych zastosowaniem tego rozwiązania na życzenie, jest to opcja i używa wyrażenia Lambda. To samo wyrażenie Lambda można zastosować również do filtra globalnego wspomnianego przez blak3r. Ta metoda wydaje się wymagać. NET 4.5.
String url = "https://www.stackoverflow.com";
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
W. NET 4.0 Wyrażenie Lambda może być zastosowane do filtra globalnego jako takiego
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-16 21:22:18
To mi pomogło:
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true; // **** Always accept
};
Fragment stąd: http://www.west-wind.com/weblog/posts/2011/Feb/11/HttpWebRequest-and-Ignoring-SSL-Certificate-Errors
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-29 22:38:35
Istnieje również krótkie rozwiązanie delegata:
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-13 12:57:27
Wspomniano, że przed. NET 4.5 właściwość na żądanie dostępu do jej ServicePointManager
nie była dostępna.
Oto kod. NET 4.0, który da ci dostęp do ServicePoint
na podstawie każdego żądania. Nie daje to dostępu do wywołania zwrotnego na żądanie, ale powinno pozwolić Ci dowiedzieć się więcej szczegółów na temat problemu. Wystarczy uzyskać dostęp do Właściwości scvPoint.Certificate
(lub ClientCertificate
, jeśli wolisz).
WebRequest request = WebRequest.Create(uri);
// oddity: these two .Address values are not necessarily the same!
// The service point appears to be related to the .Host, not the Uri itself.
// So, check the .Host vlaues before fussing in the debugger.
//
ServicePoint svcPoint = ServicePointManager.FindServicePoint(uri);
if (null != svcPoint)
{
if (!request.RequestUri.Host.Equals(svcPoint.Address.Host, StringComparison.OrdinalIgnoreCase))
{
Debug.WriteLine(".Address == " + request.RequestUri.ToString());
Debug.WriteLine(".ServicePoint.Address == " + svcPoint.Address.ToString());
}
Debug.WriteLine(".IssuerName == " + svcPoint.Certificate.GetIssuerName());
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-06 17:43:37
Na podstawie odpowiedzi Adama i komentarza Roba użyłem tego:
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => certificate.Issuer == "CN=localhost";
Który nieco filtruje "ignorowanie". W razie potrzeby można oczywiście dodać innych emitentów. Zostało to Przetestowane w. NET 2.0, ponieważ musimy obsługiwać niektóre starsze kody.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-08 07:12:03
Nawiasem mówiąc, jest to najmniej gadatliwy sposób wyłączenia wszystkich sprawdzania poprawności certyfikatu w danej aplikacji, o której wiem:
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-23 16:06:52
Dodając do odpowiedzi Sani i blak3r, dodałem następujący kod startowy dla mojej aplikacji, ale w VB:
'** Overriding the certificate validation check.
Net.ServicePointManager.ServerCertificateValidationCallback = Function(sender, certificate, chain, sslPolicyErrors) True
Wygląda na to, że działa.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-05 22:15:35
Miałem ten sam problem, więc stworzyłem klasę niż pomoc z moimi kodami. Ale użyj HttpClient:
Https://github.com/PicolotoLF/IgnoresSSL
Przykład:
public async Task DoRequest()
{
IgnoreSSL Client = new IgnoreSSL();
var client = Client.IgnoreSSLA();
//NOTE(picoloto): Use how a HttpClient
var response = await client.GetAsync(URL)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-30 22:48:31
Wskazówka: Możesz również użyć tej metody do śledzenia certyfikatów, które wkrótce wygasną. Może to uratować twój bekon, jeśli odkryjesz cert, który wkrótce wygaśnie i możesz go naprawić na czas. Dobre również dla firm trzecich - dla nas jest to DHL / FedEx. DHL po prostu niech Cert wygasa, który jest Pieprzenie nas 3 dni przed Świętami Dziękczynienia. Na szczęście jestem w pobliżu, by to naprawić ... tym razem!
private static DateTime? _nextCertWarning;
private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
if (error == SslPolicyErrors.None)
{
var cert2 = cert as X509Certificate2;
if (cert2 != null)
{
// If cert expires within 2 days send an alert every 2 hours
if (cert2.NotAfter.AddDays(-2) < DateTime.Now)
{
if (_nextCertWarning == null || _nextCertWarning < DateTime.Now)
{
_nextCertWarning = DateTime.Now.AddHours(2);
ProwlUtil.StepReached("CERT EXPIRING WITHIN 2 DAYS " + cert, cert.GetCertHashString()); // this is my own function
}
}
}
return true;
}
else
{
switch (cert.GetCertHashString())
{
// Machine certs - SELF SIGNED
case "066CF9CAD814DE2097D367F22D3A7E398B87C4D6":
return true;
default:
ProwlUtil.StepReached("UNTRUSTED CERT " + cert, cert.GetCertHashString());
return false;
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-21 20:35:34