无法使用安全链接(即https:// ......)访问.NET Core API。使用不安全的链接(即http:// ....)访问相同的API时,可以访问该API

时间:2019-10-15 09:57:45

标签: c# .net rest api .net-core

我有一个.NET Core Controller API。

  • 通过安全链接(即https:/// api / values)访问Web方法时,它不起作用

  • 如果我使用不安全的链接(即http:/// api / values)访问它,则工作正常。

请告知我是否需要对Startup.cs或appsettings.json进行一些设置

访问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;
        }

1 个答案:

答案 0 :(得分:1)

添加ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

解决了问题,我从.NET Core API中获取了数据。

注意 :我在通过Miscrosoft Word VSTO-AddIn访问.NET Core Controller API时遇到问题

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;
        }