简体   繁体   English

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

[英]'Failed to authenticate HTTPS connection' while accessing ASP.NET Core 2.1 Web API from React Native

I have an ASP.NET Core 2.1 Web API project that is serving data from an SQL Server database.我有一个 ASP.NET Core 2.1 Web API 项目,它提供来自 SQL Server 数据库的数据。 I checked 'Configure for HTTPS' option while I was creating the API project in Visual Studio.我在 Visual Studio 中创建 API 项目时选中了“配置 HTTPS”选项。

I need to access this API from a React Native project, and I have tried using the Fetch API for it.我需要从 React Native 项目访问这个 API,我尝试使用Fetch API。 The method I am using works when I make REST requests to different APIs but it is not working for my own API project.当我向不同的 API 发出 REST 请求时,我使用的方法有效,但它不适用于我自己的 API 项目。 When I make a simple GET request to the API, I receive a Network request failed error on my mobile device, while on the API end, following error stack is encountered:当我向 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)

However, the request works fine when I make it using Postman, and following is the result:但是,当我使用 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

Additional info:附加信息:

When I run the API project, following dialogue box appears:当我运行 API 项目时,会出现以下对话框:

您想信任 ASP.NET Core SSL 证书吗

Clicking Yes results in:单击“是”会导致:

访问控制列表 (ACL) 结构无效

What is the reason for this error and how should I go about solving this issue?这个错误的原因是什么,我应该如何解决这个问题?

According to this MSDN blog you can install the certificate manually.根据此 MSDN 博客,您可以手动安装证书。

  1. Open a blank Microsoft Management Console by clicking Start, then Run, entering "mmc" and clicking OK:单击开始,然后单击运行,输入“mmc”并单击确定,打开一个空白的 Microsoft 管理控制台:
  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"选择具有以下属性的证书:颁发给 = "localhost"、颁发者 = "localhost"、友好名称 = "IIS Express 开发证书"
  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:单击 DER 编码的二进制 X.509 (.CER),然后单击下一步:
  13. Enter the path for exported certificate, eg "c:\\users\\robert\\desktop\\iisexpress.cer", and then click Next:输入导出证书的路径,例如“c:\\users\\robert\\desktop\\iisexpress.cer”,然后点击下一步:
  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:在 Console Root 中,展开 Certificates (Local Computer),然后展开 Trusted Root Certification Authorities,然后单击 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, eg "c:\\users\\robert\\desktop\\iisexpress.cer", and then click Next:输入导出证书的路径,例如“c:\\users\\robert\\desktop\\iisexpress.cer”,然后单击下一步:
  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":您的 IIS Express 证书现在应该在受信任的根证书颁发机构列表中显示为“localhost”:

this my solution这是我的解决方案

dotnet dev-certs https
dotnet dev-certs https --trust

此解决方案解决了问题https://stackoverflow.com/a/59539907/8722913

dotnet dev-certs https --clean

dotnet dev-certs https --trust

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Azure Active Directory验证ASP.NET Core 2.1 Web API和使用Entity Framework验证Azure SQL - Authenticate ASP.NET Core 2.1 Web API with Azure Active Directory and Azure SQL with Entity Framework 使用 Asp.net 核心 Web Api 反应本机 - React Native with Asp.net Core Web Api Asp.net core 2.1 web api中的自定义授权 - Custom Authorization in Asp.net core 2.1 web api 如何在asp.net core 2中验证facebook web api - How to authenticate facebook web api in asp.net core 2 ASP.NET Core Restful Web API Https - 客户端连接证书错误 - ASP.NET Core Restful Web API Https - Client Connection Certificate Errors 如何使用提取将JavaScript中的FormData发送到ASP.NET Core 2.1 Web API - How to send FormData from javascript to ASP.NET Core 2.1 web API using fetch 在 ASP.NET 内核 Web API 中访问和使用 UserClaims - Accessing and using UserClaims in ASP.NET Core Web API ASP.NET Core 2.1 在 App Engine 中没有 HTTP/HTTPS 重定向 - ASP.NET Core 2.1 no HTTP/HTTPS redirection in App Engine ASP.net Core Web API localdb连接字符串异常 - ASP.net Core Web api localdb connection string exception 从 React 应用程序访问 asp.net 核心 api(均已容器化) - Accessing asp.net core api from react app (both containerized)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM