Turning around: Resolving Error "Basic Connection closed: failed to establish trust relationship for SSL/TLS security channel ."

Source: Internet
Author: User
When I write a program today, I call a third-party DLL file. The local debugging is normal, but the program does not always prompt a BUG after it arrives at the server: "The basic connection has been closed: failed to establish trust relationship for SSL/TLS Security Channel ".
Later, the DLL file was decompiled and an error occurred while obtaining the request. Reference WebResponse response = WebRequest. Create ("https ://...... "). GetResponse ();

Then, open the above address in the browser on the server and a confirmation certificate window will pop up, which seems to be a certificate problem.
After a search on the Internet, I found that it is easy to use and add a line of code before the request. C # code

  1. ServicePointManager. CertificatePolicy = new AcceptAllCertificatePolicy ();
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();

The AcceptAllCertificatePolicy must be defined by yourself: C # code

  1. Internal class AcceptAllCertificatePolicy: ICertificatePolicy
  2. {
  3. Public AcceptAllCertificatePolicy ()
  4. {
  5. }
  6. Public bool CheckValidationResult (ServicePoint sPoint,
  7. X509Certificate cert, WebRequest wRequest, int certProb)
  8. {
  9. // Always accept
  10. Return true;
  11. }
  12. }
internal class AcceptAllCertificatePolicy : ICertificatePolicy    {        public AcceptAllCertificatePolicy()        {        }        public bool CheckValidationResult(ServicePoint sPoint,           X509Certificate cert, WebRequest wRequest, int certProb)        {            // Always accept            return true;        }    }

The preceding method solves the problem, but in VS, it will prompt that ServicePointManager. CertificatePolicy has been rejected. As I like perfection, follow the prompts to use a new method.
The transformed code is more concise and clear C # code

  1. ServicePointManager. ServerCertificateValidationCallback = ValidateServerCertificate;
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;

C # code

  1. Private bool ValidateServerCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  2. {
  3. Return true;
  4. }
private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)        {            return true;        }

That's it! A commission is done!

From: http://radiumwong.iteye.com/blog/684118

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.