【发布时间】:2020-03-04 03:52:27
【问题描述】:
我有一个使用 dotnet core 的 2 个项目的解决方案
WebAPI 项目 -> REST API
MVC 项目 -> 连接到 API 以获取/发布值
我有一个核心库类,它具有连接 API 的 Helper:
public abstract class APIHelper
{
protected HttpClient ApiClient;
public APIHelper()
{
InitializeClient();
}
private void InitializeClient()
{
ApiClient = new HttpClient();
//ApiClient.BaseAddress = new Uri("https://localhost:44333/api");
ApiClient.BaseAddress = new Uri("https://localhost:5001/api");
ApiClient.DefaultRequestHeaders.Accept.Clear();
ApiClient.DefaultRequestHeaders.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
}
我有一个继承自该类并调用 API 的类:
public async Task<IEnumerable<Category>> Get()
{
using (HttpResponseMessage response = await ApiClient.GetAsync("api/Category"))
{
if (response.IsSuccessStatusCode)
{
var readed = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<IEnumerable<Category>>(readed);
return result;
}
else
{
throw new Exception();
}
}
}
如果我使用 Visual Studio 并运行 IIS 服务器,API 使用 ApiClient.BaseAddress = new Uri("https://localhost:44333/api"); 它运行完美,我可以使用 Helper 连接到 API,然后从 MVC 项目中调用 helper 类。
问题是当我使用dotnet run命令时,API运行在5001端口,MVC项目运行在5003端口,所以我调用URI:ApiClient.BaseAddress = new Uri("https://localhost:5001/api");。
当我这样做并尝试在我的浏览器中打开https://localhost:5003(MVC 项目)时,我发现错误:
An unhandled exception occurred while processing the request.
AuthenticationException: The remote certificate is invalid according to the validation procedure.
System.Net.Security.SslStream.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, ExceptionDispatchInfo exception)
HttpRequestException: The SSL connection could not be established, see inner exception.
System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
我尝试使用 http 而不是 https,结果相同。
我尝试使用不同的浏览器得到相同的结果。
我在 Windows 和 Linux 上尝试过,结果相同。
当我使用 Postman 进行尝试时,它会获取值,因此 WebPI 项目没有问题(我认为如此)。
我不知道该怎么办。
干杯。
【问题讨论】:
-
你可以尝试执行这个命令:'dotnet dev-certs https --trust' 吗?
标签: .net-core asp.net-core-webapi