远程服务器上的IIS上的远程主机强行关闭了现有连接

发布于 2025-02-10 21:27:38 字数 1971 浏览 3 评论 0原文

例外:
system.net.http.httprequestexception:无法建立SSL连接,请参见内部异常。
---> system.io.ioexception:无法从传输连接中读取数据:现有连接被远程主机强行关闭。
---> system.net.sockets.socketException(10054):现有连接被远程主机强行关闭。
---内部异常堆栈跟踪的结尾---

为ASP .NET Core 6 MVC项目获取此错误。

快速背景:最初,我将这个项目托管在远程机器上的IIS上,并在上周使其正常工作。

这具有以下代码:

builder.Services.AddHttpClient<ISampleClient, SampleClient>()
// Temporary for server
.ConfigurePrimaryHttpMessageHandler(sp =>
{
    HttpClientHandler clientHandler = new HttpClientHandler();
    clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
    return clientHandler;
});

注意 servercertificatificatecustomvalidationCallback

SampleClient类是:

public SampleClient(HttpClient httpClient, IMapper mapper, IConfiguration config)
{
    _httpClient = httpClient;
    _mapper = mapper;
    _config = config;
    _httpClient.BaseAddress = new Uri(_config["BaseAddress"]);
}

public async Task<Sample> GetSampleAsync()
{
    return await GetAsync<Sample>("api/Sample");
}

private async Task<T> GetAsync<T>(string url)
{
    _httpClient.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
    _httpClient.DefaultRequestHeaders.Add("Keep-Alive", "3600");
    return await _httpClient.GetFromJsonAsync<T>(url);
}

配置中的基础地址只是它在绑定中定义的IIS中托管的域。如前所述,我现在将ServerCertificatificatificateCustomValidationCallback设置为我们现在要绕过SSL。这仅用于测试和内部演示。

API控制器在同一项目中。托管时,可以达到API并返回数据。

我上周出版了,但是有了与Localhost的绑定,这有效。但是,我们的下属团队希望将其暴露于网络中的其他计算机,他们对绑定进行了更改(其中大部分是相同的,除了主机名之外)。显然引起了这个问题,但我没有100%相信。

我尝试了与显示的错误消息有关的多个建议,即在编辑安全协议时,即使缺乏理解,我也在sampleclient类中添加了代码。

我想补充一下,在本地机器上,它没有问题,但是我具有Local -Host绑定。我认为这是因为IIS中的自定义域?

我希望获得更多建议以解决这个问题。

编辑:其他信息,使用主机名发布时它失败了,但是当绑定为https:// localhost时,它起作用了!

Exception:
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..
---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
--- End of inner exception stack trace ---

Getting this error for an ASP .NET Core 6 MVC Project.

Quick Background: I initially had this project hosted on IIS on a remote machine and got it working last week.

This has the following code:

builder.Services.AddHttpClient<ISampleClient, SampleClient>()
// Temporary for server
.ConfigurePrimaryHttpMessageHandler(sp =>
{
    HttpClientHandler clientHandler = new HttpClientHandler();
    clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
    return clientHandler;
});

Note the ServerCertificateCustomValidationCallback.

And the SampleClient class is:

public SampleClient(HttpClient httpClient, IMapper mapper, IConfiguration config)
{
    _httpClient = httpClient;
    _mapper = mapper;
    _config = config;
    _httpClient.BaseAddress = new Uri(_config["BaseAddress"]);
}

public async Task<Sample> GetSampleAsync()
{
    return await GetAsync<Sample>("api/Sample");
}

private async Task<T> GetAsync<T>(string url)
{
    _httpClient.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
    _httpClient.DefaultRequestHeaders.Add("Keep-Alive", "3600");
    return await _httpClient.GetFromJsonAsync<T>(url);
}

The Base address in config is just the domain it is hosted in IIS defined in bindings. As mentioned, I set ServerCertificateCustomValidationCallback as we want to bypass SSL for now. This is just for testing and to demo internally.

The API Controller is in this same project. When hosted, the API can be reached and returns data.

I have published this last week, but with bindings to localhost, that worked. However, our infra team wanted to expose this to other computers in the network, they made changes to the bindings (of which are mostly the same except the hostname). Apparently causing the issue, but I'm not 100% convinced.

I have tried multiple suggestions relating to the error message shown, in editing the Security Protocol, even though there was a lack of understanding, I have added the code in the SampleClient class.

I would like to add, on my local machine, it works with no problems, but I have the localhost binding. I think it's because of the custom domain in IIS?

I'm hoping to get more suggestions to get this fixed.

EDIT: Additional Information, when publishing using the hostname https://svdb01.sample.com.au, it fails, but when the binding is https://localhost, it works!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文