c# - 请求被中止:无法创建 SSL/TLS 安全通道,但正在使用 Postman / CURL
问题描述
我正在拼命地尝试与使用.NET 的服务器进行通信。
- 操作系统:Windows 10 版本 1903
- 框架:4.7.2
- 网站的 TLS 版本:根据 Google Chrome 安全标签的 TLS1.2
- 我试图通过添加默认设置为 true 的 ServerCertificateValidationCallback 方法来绕过服务器 ssl 验证,但我同意,这不是要做的事情,它只是为了测试目的而做的。
- 根据 Chrome:TLS 1.2、带有 P-256 的 ECDHE_RSA 和 AES_256_GCM,使用的密码是这个。
- 正如评论中提到的@mjwillis,ssllabs.com/ssltest/analyze.html?d=oauth2-apiii.ei.com 和 docs.microsoft.com/en-us/windows/win32/secauthn/... 建议他们应该有 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384共同点。
我的代码相对简单,我使用的是来自 .NET Core 的最新 httpClient 类和 Framework 4.7.2(Windows 10 版本 1903):
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://oauth2-apiii.e-i.com"))
{
request.Headers.TryAddWithoutValidation("Accept", "*/*");
var contentList = new List<string>();
contentList.Add($"grant_type={Uri.EscapeDataString("authorization_code")}");
contentList.Add($"code={Uri.EscapeDataString("a")}");
contentList.Add($"scope={Uri.EscapeDataString("b")}");
contentList.Add($"client_id={Uri.EscapeDataString("c")}");
contentList.Add($"code_verifier={Uri.EscapeDataString("d")}");
request.Content = new StringContent(string.Join("&", contentList));
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = httpClient.SendAsync(request).GetAwaiter().GetResult();
HttpContent httpContent = response.Content;
string test = httpContent.ToString();
}
}
}
}
}
我只收到以下错误:请求被中止:无法创建 SSL/TLS 安全通道。
我尝试了其他方法(HttpRequest,RestSharp),但它仍然不起作用。
我尝试使用 cURL 和 Postman,结果还可以,我从服务器得到了正确的结果。
cURL/Postman 怎么可能设法向这个 URL 发送请求,而 .NET 却不能呢?
编辑:
这是 Fiddler .NET 隧道跟踪:
[
这是 Fiddler Postman 隧道跟踪:
[
我看不到明显的区别,似乎双方使用的密码相同:TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
编辑 2:有趣的事实:Internet Explorer 和 Microsoft Edge(不是 Chromium)都无法访问 URL:https ://oauth2-apiii.ei.com/sandbox/cic/oauth2而 Firefox、Edge Chromium 和 Chrome 没有困难.
我想这个问题可能是相关的。
解决方案
推荐阅读
- scala - 为什么不同名称的案例类和对象出现在同一个scala文件中?
- sql - 拆分要在 IN 运算符中使用的字符串
- excel - 循环以将工作表保存在新工作簿中
- scala - 如何在 Scala 中创建不可变的双链表?
- r - ans[npos] <- rep(no, length.out = len)[npos] 中的错误:替换的长度为零
- r - For循环对r中数据框列中的值求和
- javascript - 当 html 在表单中时,我的 JavaScript 不运行
- ios - 为什么 cocoapods 不能在 Apple Silicon 上工作?
- opencv-python - 导入 cv2 错误未定义符号:PyCMethod_New
- ansible - 如何在单个或简短的主机列表上运行剧本并从主机不属于的清单组中获取内容?