【发布时间】:2018-08-29 07:36:40
【问题描述】:
我有一个 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 项目时,会出现以下对话框:
单击“是”会导致:
这个错误的原因是什么,我应该如何解决这个问题?
【问题讨论】:
-
您需要先修复“附加信息”部分 - 这会将本地服务器使用的证书安装到您的计算机上。信任库中没有证书,没有信任,反应上面的本机错误。
-
@John:有什么办法可以解决“ACL 结构无效”的问题吗?
-
让我知道我编辑的答案是否有效 :)
-
@UzairA。你能解决问题吗。能否请您分享解决方案。我面临同样的问题。谢谢
-
@ramkris 抱歉,没有。我放弃了这个问题,最终使用了完全不同的后端策略。
标签: c# asp.net .net react-native asp.net-core