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

问题描述 投票:2回答:1

我有一个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

单击是会导致:

Access Control List (ACL) structure is invalid

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

c# asp.net .net react-native asp.net-core
1个回答
1
投票

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

  1. 单击“开始”,然后单击“运行”,输入“mmc”并单击“确定”,打开空白Microsoft管理控制台:
  2. 单击“文件”,然后单击“添加/删除管理单元”:
  3. 显示“添加或删除管理单元”对话框时,单击“证书”,然后单击“添加”:
  4. 显示“证书管理单元”对话框时,单击“计算机帐户”,然后单击“下一步”:
  5. 单击本地计算机,然后单击完成:
  6. 单击“确定”关闭“添加或删除管理单元”对话框:
  7. 在控制台根目录中,展开“证书(本地计算机)”,然后展开“个人”,再单击“证书”:
  8. 选择具有以下属性的证书:Issued to =“localhost”,Issued by =“localhost”,Friendly Name =“IIS Express Development Certificate”
  9. 单击“操作”,然后单击“所有任务”,再单击“导出”:
  10. 显示“证书导出向导”时,单击“下一步”:
  11. 单击否,不导出私钥,然后单击下一步:
  12. 单击DER编码二进制X.509(.CER),然后单击“下一步”:
  13. 输入导出证书的路径,例如“c:\ users \ robert \ desktop \ iisexpress.cer”,然后单击“下一步”:
  14. 单击“完成”以导出证书:
  15. 当“证书导出向导”显示一个对话框,通知您导出成功时,单击“确定”:
  16. 在控制台根目录中,展开“证书(本地计算机)”,然后展开“受信任的根证书颁发机构”,再单击“证书”:
  17. 单击“操作”,然后单击“所有任务”,再单击“导入”:
  18. 显示“证书导入向导”时,单击“下一步”:
  19. 输入导出证书的路径,例如“c:\ users \ robert \ desktop \ iisexpress.cer”,然后单击“下一步”:
  20. 确保选中“将所有证书放在以下存储中”并验证所选证书存储区是否设置为“受信任的根证书颁发机构”,然后单击“下一步”:
  21. 单击“完成”以导入证书:
  22. 当证书导入向导显示一个对话框,通知您导入成功时,单击确定:
  23. 您现在应将IIS Express证书作为“localhost”显示在受信任的根证书颁发机构列表中:
© www.soinside.com 2019 - 2024. All rights reserved.