Jul 4, 2011

The remote certificate is invalid

The remote certificate is invalid according to the validation procedure


you will get this error because of certificates issue while getting the response from the external web service or any other Web Request from the server.

follow the below snippet to resolve this issue

using System.Net;

using System.Net.Security;

using System.Security.Cryptography.X509Certificates;

protected void Page_Load(object sender, EventArgs e)

    {

      ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCB;

      Uri uri = new Uri("http://microsoft.com");

      if (uri.Scheme == Uri.UriSchemeHttp)

     {

       HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

       request.Method = WebRequestMethods.Http.Get;

       HttpWebResponse response = (HttpWebResponse)request.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());     

       string tmp = reader.ReadToEnd();

Response.Write(tmp);

       response.Close();

               

     }

       

    }

public static bool RemoteCertificateValidationCB(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)

    {

        //If it is really important, validate the certificate issuer here.

        string resultsTrue = certificate.Issuer.ToString();

        //For now, accept any certificate

        return true;

    }




2 comments:

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
Vee Eee Technologies| Vee Eee Technologies|

thanks for ur comment . i will publish more useful articles in this blog

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More