AnsweredAssumed Answered

HCP API for DotNet Core

Question asked by Praveen b KP on Jan 22, 2018
Latest reply on Apr 4, 2018 by Praveen b KP

I have been using HCP API for some of my .Net applications. I have a common .Net library which does the HCP API calls for these apps. It works as expected. I recently had to use it for a .NetCore 2 in a application and the HCP API calls from this app failed with security error. Below is the sample code from the library that i have been using.

 

            var client = new HttpClient();

        

            client.DefaultRequestHeaders.Accept.Clear();

            client.DefaultRequestHeaders.Clear();

            client.DefaultRequestHeaders.Accept.Add(

                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(

                    "application/vnd.github.v3+json"

                    ));

            client.DefaultRequestHeaders.Add("User-Agent",

                ".NET Foundation Repository Reporter");

            client.DefaultRequestHeaders.Add("Authorization", GetAuthorizationHeaderValue());

            client.Timeout = TimeSpan.FromSeconds(90);

            client.BaseAddress = new Uri("https://somehcpnamespace.net");

            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

            //SecurityProtocolType.Ssl3 has been removed from below line as it's obsolete

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            ServicePointManager.DefaultConnectionLimit = 10;

            ServicePointManager.Expect100Continue = true;

 

            try

            {

                using (HttpResponseMessage res = await client.SendAsync(new HttpRequestMessage(

                    HttpMethod.Head, "/rest/"

                    ), HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))

                {

                    if (res != null && res.IsSuccessStatusCode)

                    {

                        Console.WriteLine("response");

                    }

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

 

Error message: An error occurred while sending the request. Innerexception message: A security error occurred. StackTrace: at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Threading.Tasks.RendezvousAwaitable`1.GetResult() at System.Net.Http.WinHttpHandler.d__105.MoveNext()

 

Please suggest what changes has to be done for it to work for .NetCore Apps or a link on this would be great.

 

Thanks,

Praveen

Outcomes