0

I've a .NET Core Controller API.

  • When accessing the Web Method(s) through secured link, i.e. https:///api/values, it's not working

  • If I'm accessing it with unsecured link, i.e. http:///api/values, it's working fine.

Please let me know if I need to do something into my Startup.cs or appsettings.json

Sample Code to access the API:

try { string serviceUrl= "https://domainname/api/values"; HttpClient client = new HttpClient(); HttpResponseMessage response = client.GetAsync(serviceUrl).Result; string stringData = response.Content.ReadAsStringAsync().Result; } catch (Exception ex) { throw ex; } 
6
  • What do you mean by it's not working? Commented Oct 15, 2019 at 9:59
  • Hello Chetan, Thanks for your reply. It's throwing exception when trying to access it. Message: The underlying connection was closed: An unexpected error occurred on a send. Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. Regards, Ashish Commented Oct 15, 2019 at 10:07
  • Are you using IIS to browse your code locally? because if that is the case then http makes sense. Commented Oct 15, 2019 at 10:16
  • Write System.Net.ServicePointManager.SecurityProtocol as first line in try block Commented Oct 15, 2019 at 10:17
  • I'm publishing my code on Azure. After publish, if I'm making my application secured i.e. https - When I'm trying to access it (through a client app. in my case it's VSTO Add-In), throws exception. - If I'm not securing the application/ site and accessing it, it's working. Commented Oct 15, 2019 at 10:27

1 Answer 1

1

Adding ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

solves the issue and I get the data back from my .NET Core API.

Note: I was facing issue in accessing .NET Core Controller API through Miscrosoft Word VSTO-AddIn

try { /*This is the Line I added and it makes my .NET Core API Accessible in my VSTO-AddIn */ ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; string serviceUrl= "https://domainname/api/values"; HttpClient client = new HttpClient(); HttpResponseMessage response = client.GetAsync(serviceUrl).Result; string stringData = response.Content.ReadAsStringAsync().Result; } catch (Exception ex) { throw ex; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.