I have a piece of code that works in a VS.C# project, but when this code is part of a webPart in Sharepoint, it throws an error.
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(validateUrl);
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Validate url is a HTTPS-uri.
In Sharepoint, when I add the first rule it throws an exception when I go to the page and says
Object reference not set to instance of an object.
I do not exactly know where it throws this exception because this piece of code is being inclosed by a Try { } Catch (Exception e) -block, but that one isn't fired. So that's kind of weird.
If I let out the first rule, it throws an exception (in the right catch-block):
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
Which is the same Exception I get if I let out the first rule in the VS.C# project.
Any ideas? Or other solutions? This is obviously not the safest approach.
By adding the server certificate in the "Trusted Root Certification Authorities" store (through MMC.exe) the problem was solved without the use of accepting all certificates.