【问题标题】:.NET Core API not accessible using secured link (i.e. https://......). Same API is accessible when accessing it with unsecured link (i.e. http://....).NET Core API 无法使用安全链接(即 https://......)访问。使用不安全的链接(即 http://....)访问时可以访问相同的 API
【发布时间】:2019-10-15 09:57:45
【问题描述】:

我有一个 .NET Core 控制器 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;
        }

【问题讨论】:

  • it's not working 是什么意思?
  • 您好 Chetan,感谢您的回复。尝试访问它时抛出异常。消息:底层连接已关闭:发送时发生意外错误。无法从传输连接读取数据:现有连接被远程主机强行关闭。问候,Ashish
  • 您是否使用IIS 在本地浏览您的代码?因为如果是这样,那么http 是有道理的。
  • System.Net.ServicePointManager.SecurityProtocol作为try块的第一行
  • 我在 Azure 上发布我的代码。发布后,如果我使我的应用程序安全,即 https - 当我尝试访问它时(通过客户端应用程序。在我的情况下,它是 VSTO 插件),抛出异常。 - 如果我没有保护应用程序/站点并访问它,它就可以工作。

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


【解决方案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;
        }

【讨论】:

    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 2016-01-26
    • 2011-01-08
    • 2015-05-06
    • 2017-09-01
    • 2017-11-28
    相关资源
    最近更新 更多