Could not create ssl/tls secure channel. c#

Rajar Ahmed
Rajar Ahmed used Ask the Experts™
on
I am receiving the following error always when this method is executed through Window Service whether when the same code is executed through Web Portal it is working Fine.

Could not create ssl/tls secure channel. c#


using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Net;
using Newtonsoft.Json.Linq;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;


 void wc1TwoSample2()
        {
            try
            {
                ServicePointManager.ServerCertificateValidationCallback +=
                delegate(Object sender1, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.DefaultConnectionLimit = 9999;
                //ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(delegate { return true; });
                string URL = "https:URL";
                string param = "/api/v1/accounts";
                string resp = URL + param;
                var http = (HttpWebRequest)WebRequest.Create(new Uri(resp));
                http.Accept = "application/json";
                http.ContentType = "application/json";
                http.Method = "POST";
                http.Headers.Add("Authorization", "Bearer 23123123");
                classa obj = new classa();
                obj.prop1 = "3123";
                obj.prop2 = "fsdfsdf";
                obj.prop3 = "sdffsdf";
                string jsonoutput = JsonConvert.SerializeObject(obj);
                string parsedContent = jsonoutput;
                ASCIIEncoding encoding = new ASCIIEncoding();
                Byte[] bytes = encoding.GetBytes(parsedContent);
                System.IO.Stream newStream = http.GetRequestStream();
                newStream.Write(bytes, 0, bytes.Length);
                newStream.Close();
                var response = http.GetResponse();
                var stream = response.GetResponseStream();
                var sr = new System.IO.StreamReader(stream);
                var content = sr.ReadToEnd();
            }
            catch (Exception ex)
            {

            }
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
If the web portal is on a different computer  then some certificates may be missing. If both computers have the same certificates, verify the permissions.

Author

Commented:
Apologies for late reply, we found that api link is showing page cannot be displayed when it is opened directly to that particular server.

How to white list this url ?
Internet explorer didnot load the api url in the server and we used the different server due to time constraints .  Thanks for your suggestion.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial