我有一个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项目时,出现以下对话框:
单击“是”将导致:
此错误的原因是什么,如何解决该问题?
答案 0 :(得分:1)
根据this MSDN blog,您可以手动安装证书。
- 通过单击“开始”,然后单击“运行”,输入“ mmc”并单击“确定”,打开空白的Microsoft管理控制台:
- 单击“文件”,然后单击“添加/删除管理单元”:
- 显示“添加或删除管理单元”对话框时,单击“证书”,然后单击“添加”:
- 显示“证书管理单元”对话框时,单击“计算机帐户”,然后单击“下一步”:
- 单击“本地计算机”,然后单击“完成”:
- 单击“确定”关闭“添加或删除管理单元”对话框:
- 在控制台根目录中,展开“证书(本地计算机)”,再展开“个人”,然后单击“证书”:
- 选择具有以下属性的证书:颁发给=“ localhost”,颁发者=“ localhost”,友好名称=“ IIS Express开发证书”
- 单击“操作”,然后单击“所有任务”,然后单击“导出”:
- 显示“证书导出向导”时,单击“下一步”:
- 单击“否,不导出私钥”,然后单击“下一步”:
- 单击DER编码的二进制X.509(.CER),然后单击“下一步”:
- 输入导出证书的路径,例如“ c:\ users \ robert \ desktop \ iisexpress.cer”,然后单击“下一步”:
- 单击“完成”以导出证书:
- 当证书导出向导显示一个对话框通知您导出成功时,单击“确定”:
- 在控制台根目录中,展开“证书(本地计算机)”,然后展开“受信任的根证书颁发机构”,然后单击“证书”:
- 单击“操作”,然后单击“所有任务”,然后单击“导入”:
- 显示“证书导入向导”时,单击“下一步”:
- 输入导出证书的路径,例如“ c:\ users \ robert \ desktop \ iisexpress.cer”,然后单击“下一步”:
- 确保选中“将所有证书放置在以下存储中”,并确认所选的证书存储设置为“受信任的根证书颁发机构”,然后单击“下一步”:
- 单击“完成”以导入证书:
- 当证书导入向导显示一个对话框通知您导入成功时,单击“确定”:
- 您的IIS Express证书现在应在“受信任的根证书颁发机构”列表中显示为“ localhost”:
答案 1 :(得分:0)
这是我的解决方案
dotnet dev-certs https
dotnet dev-certs https --trust
答案 2 :(得分:0)
此解决方案解决了问题 https://stackoverflow.com/a/59539907/8722913
dotnet dev-certs https --clean
dotnet dev-certs https --trust