Different implementations of HTTPS requests under asp.net1.1 and 2.0

Source: Internet
Author: User
Tags bool
Asp.net| Request

There was a debugging error in the HTTPS request for today's project, internal error: Failed to establish a trust relationship for the SSL/TLS secure channel. Error page: The remote certificate is not valid according to the authentication process. After analysis, open in the browser to make a security confirmation. This is the problem that this dialog box causes. Search the Internet for a general solution, but the content of the search is relatively small, now summed up here.

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Net;
Using System.IO;
Using System.Text;
Using System.Net.Security;
Using System.Security.Authentication;
Using System.Security.Cryptography.X509Certificates;

public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
For 1.1 under 2.0 ServicePointManager.CertificatePolicy already obsolete
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy ();
For 2.0
Servicepointmanager.servercertificatevalidationcallback = new System.Net.Security.RemoteCertificateValidationCallback (CheckValidationResult);
HttpWebRequest request = (HttpWebRequest) webrequest.create ("Https://someurl");
Request. method = ' Get ';
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
Stream Receivestream = Response. GetResponseStream ();
StreamReader Readstream = new StreamReader (Receivestream, Encoding.UTF8);
Page.Response.Write (Readstream.readtoend ());
Response. Close ();
Readstream.close ();
}
For 2.0
public bool CheckValidationResult (object sender, X509Certificate certificate, X509chain chain, sslpolicyerrors errors)
{//Always Accept
return true;
}
For 1.1
Internal class Acceptallcertificatepolicy:icertificatepolicy
{
Public AcceptAllCertificatePolicy ()
{
}

public bool CheckValidationResult (ServicePoint spoint, System.Security.Cryptography.X509Certificates.X509Certificate cert, WebRequest wrequest, int certprob)
{
Always Accept
return true;
}
}
}



Related Article

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.