Migration from http to https using TLS/SSL Certificate

Viewed 94

I have been trying to integrate a payment uat service url which is https protocol and unable to successfully implement so far

I will elaborate in detail.

I have created a wrapper service on the top of the payment service to consume it and we have been testing it in soap ui tool.The uat url when triggered directly in the soap ui tool along with credentials,header values,.pfx file and the request model is working.

please see the image below.

uat url response

this is a uat server.This is a rest api.The request is xml.

The request should contains credentials,header values and certificate(.pfx file) all these are passed in the code.

We have received error like

  1. Could not establish secure channel for SSL/TLS with authority
  2. 400 not found
  3. 500 Internal Server
  4. currently the error we are receiving is The remote server returned an error: (401) Unauthorized

application response

the code we have used so far

 private static dynamic SendRequest(string url, string ReqModel, string method, bool IsAuthorise, string UCICMethod)
        {


            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(ReqModel);

            ServicePointManager.Expect100Continue = true;
            //ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            ServicePointManager.ServerCertificateValidationCallback = (snder, cert, chain, error) => true;
           
            
            string certName = HttpContext.Current.Server.MapPath("CertificateFolder/star_muthootapps.pfx");
            string password = @"123";
            dynamic Response = null;
            HttpWebRequest serviceRequest = (HttpWebRequest)WebRequest.Create(url);
            serviceRequest.Method = method;
            serviceRequest.UseDefaultCredentials = false;
            //HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            //serviceRequest.CachePolicy = noCachePolicy;
          


            X509Certificate2 certif = new X509Certificate2(certName, password);

            //serviceRequest.ClientCertificates.Add(certificates[0]);
            serviceRequest.ClientCertificates.Add(certif);
            serviceRequest.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["serviceclientuser_YesBank"].ToString().Trim(), ConfigurationManager.AppSettings["serviceclientpass_YesBank"].ToString().Trim());
            


            if (method != "GET")
            {
                //serviceRequest.ContentType = "application/xml";
                serviceRequest.ContentType = "text/xml";
                serviceRequest.ContentLength = bytes.Length;
                Stream requestStream = serviceRequest.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();

                //using (var streamWriter = new StreamWriter(serviceRequest.GetRequestStream()))
                //{
                //    streamWriter.Write(ReqModel);
                //    streamWriter.Flush();
                //    streamWriter.Close();
                //}
            }

           

            var clientid = ConfigurationManager.AppSettings["X-IBM-Client-Id_YesBank"].ToString().Trim();
            var clientpassd = ConfigurationManager.AppSettings["X-IBM-Client-Secret_YesBank"].ToString().Trim();

            if (IsAuthorise)
            {
                serviceRequest.Headers.Add("X-IBM-Client-Id", clientid);
                serviceRequest.Headers.Add("X-IBM-Client-Secret", clientpassd);
            }


         
            HttpWebResponse serviceResponse;
            try
            {
                serviceResponse = (HttpWebResponse)serviceRequest.GetResponse();
         
                if (serviceResponse.StatusCode == HttpStatusCode.OK)
                {
                    using (var FetchedDatastreamReader = new StreamReader(serviceResponse.GetResponseStream()))
                    {
                        Response = JsonConvert.DeserializeObject<dynamic>(FetchedDatastreamReader.ReadToEnd());
                    }
                }

                return new 
                {
                    Success = false,
                    Message = "Response Failed",
                    DataResponse = Response
                };
            }
            catch (Exception ex)
            {
                return new 
                {
                    Success = false,
                    Message = ex.Message.ToString()
                };
            }
        }

When executing an exception happens in the catch block.

In the server we have done the following

We have added the certificate in certificate local store and added the pfx file in server certificates option in IIS Server and then mapped the certificate with application in IIS Server. Please check the image link below

certificate root IIS Server

Please let know what are we missing here.

Referred this link

Sending a HttpWebRequest with PFX Certificate C#

0 Answers
Related