c# - 从 React Native 访问 ASP.NET Core 2.1 Web API 时“无法验证 HTTPS 连接”

标签 c# asp.net .net react-native asp.net-core

我有一个 ASP.NET Core 2.1 Web API 项目,它提供来自 SQL Server 数据库的数据。我在 Visual Studio 中创建 API 项目时选中了“为 HTTPS 配置”选项。

我需要从 React Native 项目访问这个 API,我已经尝试使用 Fetch API 来访问它。当我向不同的 API 发出 REST 请求时,我使用的方法有效,但它不适用于我自己的 API 项目。当我向 API 发出简单的 GET 请求时,我在移动设备上收到了一个Network request failed 错误,而在 API 端,遇到了以下错误堆栈:

dbug: HttpsConnectionAdapter[1]
      Failed to authenticate HTTPS connection.
System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> System.ComponentModel.Win32Exception: An unknown error occurred while processing the certificate
   --- End of inner exception stack trace ---
   at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, ExceptionDispatchInfo exception)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Security.SslState.ThrowIfExceptional()
   at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
   at System.Net.Security.SslStream.EndAuthenticateAsServer(IAsyncResult asyncResult)
   at System.Net.Security.SslStream.<>c.<AuthenticateAsServerAsync>b__51_1(IAsyncResult iar)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Server.Kestrel.Https.Internal.HttpsConnectionAdapter.InnerOnConnectionAsync(ConnectionAdapterContext context)

但是,当我使用 Postman 发出请求时,请求工作正常,结果如下:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/api/values application/json
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[2]
      Successfully validated the token.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.1.1-rtm-30846 initialized 'DataContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (62ms) [Parameters=[@__get_Item_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
      SELECT TOP(1) [e].[id], [e].[pw], [e].[pws], [e].[em]
      FROM [ul] AS [e]
      WHERE [e].[id] = @__get_Item_0
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Route matched with {action = "Get", controller = "Values"}. Executing action Api.Controllers.ValuesController.Get (Api)
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1]
      Authorization was successful.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method Api.Controllers.ValuesController.Get (Api) - Validation state: Valid
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action method Api.Controllers.ValuesController.Get (Api), returned result Microsoft.AspNetCore.Mvc.ObjectResult in 0.4905ms.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
      Executing ObjectResult, writing value of type 'System.String[]'.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Api.Controllers.ValuesController.Get (Api) in 30.7297ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
  Request finished in 125.3707ms 200 application/json; charset=utf-8

附加信息:

当我运行 API 项目时,出现以下对话框:

Would you like to trust the ASP.NET Core SSL certificate

单击"is"会导致:

Access Control List (ACL) structure is invalid

此错误的原因是什么?我应该如何解决此问题?

最佳答案

根据 this MSDN blog您可以手动安装证书。

  1. Open a blank Microsoft Management Console by clicking Start, then Run, entering "mmc" and clicking OK:
  2. Click File, and then click Add/Remove Snap-in:
  3. When the Add or Remove Snap-ins dialog box is displayed, click Certificates, and then click Add:
  4. When the Certificates Snap-ins dialog box is displayed, click Computer account, and then click Next:
  5. Click Local computer, and then click Finish:
  6. Click OK to close the Add or Remove Snap-ins dialog box:
  7. In the Console Root, expand Certificates (Local Computer), then expand Personal, and then click Certificates:
  8. Select the certificate with the following attributes: Issued to = "localhost", Issued by = "localhost", Friendly Name = "IIS Express Development Certificate"
  9. Click Action, then click All Tasks, and then click Export:
  10. When the Certificate Export Wizard is displayed, click Next:
  11. Click No, do not export the private key, and then click Next:
  12. Click DER encoded binary X.509 (.CER), and then click Next:
  13. Enter the path for exported certificate, e.g. "c:\users\robert\desktop\iisexpress.cer", and then click Next:
  14. Click Finish to export the certificate:
  15. Click OK when the Certificate Export Wizard displays a dialog box informing you that the export was successful:
  16. In the Console Root, expand Certificates (Local Computer), then expand Trusted Root Certification Authorities, and then click Certificates:
  17. Click Action, then click All Tasks, and then click Import:
  18. When the Certificate Import Wizard is displayed, click Next:
  19. Enter the path to your exported certificate, e.g. "c:\users\robert\desktop\iisexpress.cer", and then click Next:
  20. Ensure that Place all certificates in the following store is checked and verify that the selected Certificate store is set to Trusted Root Certification Authorities, and then click click Next:
  21. Click Finish to import the certificate:
  22. Click OK when the Certificate Import Wizard displays a dialog box informing you that the import was successful:
  23. You IIS Express certificate should now be displayed in the listed of Trusted Root Certification Authorities as "localhost":

关于c# - 从 React Native 访问 ASP.NET Core 2.1 Web API 时“无法验证 HTTPS 连接”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52071987/

相关文章:

c# - 如何找到datagridview中存在的行并将其附加到datagridview的最后一行

c# ProtoBuf serializer 新类型

C# DataTable 计算某个范围内的行数

javascript - 如何使用 RadContextMenu 使用 radconfirm 窗口进行回发?

asp.net - 元标记 MSSmartTagsPreventParsing 的用途是什么?

下载电影的 C# 模式匹配

asp.net - 目录上的 "401 Unauthorized"

c# - 在运行时从另一个 AppDomain 调试动态加载的 DLL

.net - 堆栈溢出时的编辑控制

.net - 加载 View 模型在缓存 MVVM 灯中找不到 IDataErrorInfo 类型