【问题标题】:AuthenticationException WebAPI / ASP MVC dotnet CoreAuthenticationException WebAPI / ASP MVC dotnet Core
【发布时间】:2020-03-04 03:52:27
【问题描述】:

我有一个使用 dotnet core 的 2 个项目的解决方案

WebAPI 项目 -> REST API

MVC 项目 -> 连接到 API 以获取/发布值

我有一个核心库类,它具有连接 API 的 Helper:

public abstract class APIHelper
    {
        protected HttpClient ApiClient;

        public APIHelper()
        {
            InitializeClient();
        }

        private void InitializeClient()
        {
            ApiClient = new HttpClient();
            //ApiClient.BaseAddress = new Uri("https://localhost:44333/api");
            ApiClient.BaseAddress = new Uri("https://localhost:5001/api");
            ApiClient.DefaultRequestHeaders.Accept.Clear();
            ApiClient.DefaultRequestHeaders.Accept
                .Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
    }

我有一个继承自该类并调用 API 的类:

public async Task<IEnumerable<Category>> Get()
{
    using (HttpResponseMessage response = await ApiClient.GetAsync("api/Category"))
    {
        if (response.IsSuccessStatusCode)
        {
            var readed = await response.Content.ReadAsStringAsync();
            var result = JsonConvert.DeserializeObject<IEnumerable<Category>>(readed);
            return result;
        }
        else
        {
            throw new Exception();
        }

    }
}

如果我使用 Visual Studio 并运行 IIS 服务器,API 使用 ApiClient.BaseAddress = new Uri("https://localhost:44333/api"); 它运行完美,我可以使用 Helper 连接到 API,然后从 MVC 项目中调用 helper 类。

问题是当我使用dotnet run命令时,API运行在5001端口,MVC项目运行在5003端口,所以我调用URI:ApiClient.BaseAddress = new Uri("https://localhost:5001/api");

当我这样做并尝试在我的浏览器中打开https://localhost:5003(MVC 项目)时,我发现错误:

An unhandled exception occurred while processing the request.
AuthenticationException: The remote certificate is invalid according to the validation procedure.

System.Net.Security.SslStream.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, ExceptionDispatchInfo exception)
HttpRequestException: The SSL connection could not be established, see inner exception.

System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)

我尝试使用 http 而不是 https,结果相同。

我尝试使用不同的浏览器得到相同的结果。

我在 Windows 和 Linux 上尝试过,结果相同。

当我使用 Postman 进行尝试时,它会获取值,因此 WebPI 项目没有问题(我认为如此)。

我不知道该怎么办。

干杯。

【问题讨论】:

标签: .net-core asp.net-core-webapi


【解决方案1】:

您的 SSL 证书似乎有问题,您可以绕过此错误,如下所示我没有尝试您的代码,但在从 HttpClient 初始化新对象后添加以下代码时,它总是适用于我

// bypass bad SSL certificate errors
        ServicePointManager.ServerCertificateValidationCallback =
              delegate (object s, X509Certificate certificate,
                       X509Chain chain, SslPolicyErrors sslPolicyErrors)
              { return true; };

所以你的 API 助手应该如下所示

public abstract class APIHelper
{
    protected HttpClient ApiClient;

    public APIHelper()
    {
        InitializeClient();
    }

    private void InitializeClient()
    {
        ApiClient = new HttpClient();
        // bypass bad ssl certificate errors
        ServicePointManager.ServerCertificateValidationCallback =
              delegate (object s, X509Certificate certificate,
                       X509Chain chain, SslPolicyErrors sslPolicyErrors)
              { return true; };
        //ApiClient.BaseAddress = new Uri("https://localhost:44333/api");
        ApiClient.BaseAddress = new Uri("https://localhost:5001/api");
        ApiClient.DefaultRequestHeaders.Accept.Clear();
        ApiClient.DefaultRequestHeaders.Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
    }
}

【讨论】:

【解决方案2】:

当您使用visual studio进行调试时,它使用iisexpress作为默认启动选项,并为iisexpress配置了端口和SSL证书。

然后,Iisexpress 将根据您使用的 dotnet 版本和 web.config 中的配置,在进程中或反向代理中与 dotnet 应用程序通信。

当您使用 dotnet run 运行它时,它使用 kestrel,您需要设置端口、SSL 证书和代码中的所有内容。检查 program.cs 和 startup.cs 文件中的代码。

其中一个应该调用 usekestrel,它具有您可以配置的 kestrel 附加选项。

【讨论】:

猜你喜欢
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-04
  • 2020-07-03
  • 2020-02-12
  • 1970-01-01
  • 2021-02-12
相关资源
最近更新 更多