TLS 1.1 和 1.2 的连接问题 - Azure Web 应用
Connection issue with TLS 1.1 and 1.2 - Azure web app
在我的应用程序中,我从另一个 WebAPI 方法(理想情况下是内部服务调用)调用用 WebAPI 编写并托管在 Azure 的 PaaS 环境中的 API,假设 WebApp_A 中的 MethodA 正在调用 WebApp_B 中的 MethodB。但是如果 WebApp_B 的 TLS 设置是 1.1 或 1.2(适用于 1.0),我会收到上述错误。
"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
我有类似的 WebApp (WebApp_C),它没有 TLS 错误。下面是我们用来从 WebApp_A
调用 WebApp_B 中的 MethodB 的代码
public async Task<ServiceResponse> CreateDialog(RequestObject requestObject)
{
ServiceResponse serviceResponse = new ServiceResponse();
try
{
using (var client = new HttpClient())
{
SessionTokenBo jwtData = new SessionTokenBo();
Logger = _logger;
jwtData = GetInternalToken(InternalCallTypes.Utilities.ToString(), int.Parse(ServiceConfiguration.TokenExpiryWindow), int.Parse(ServiceConfiguration.InternalTokenExpiryWindow));
if (null != jwtData)
{
client.BaseAddress = new Uri("URI");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtData.Token); HttpResponseMessage response = await client.PostAsJsonAsync("service/methodB", requestObject);
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync().Result;
serviceResponse = JsonConvert.DeserializeObject<ServiceResponse>(data);
}
}
}
}
catch (Exception ex)
{
throw
}
return serviceResponse;
}
如果我提供这样的安全协议,它将起作用
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
我也尝试了邮递员的请求,但没有失败。所以现在很困惑,因为如果邮递员的工作正常,那么理想情况下它不是 WebApp 设置问题
TLS 1.0 不再是默认设置。在发出请求之前添加此行:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
参考这个issue。
更新:
but my doubt is why the same code is not failing for other web apps ?
发现问题,是因为 webconfig 中的目标框架是 4.5.2。
<compilation targetFramework="4.7.2"></compilation>
<httpRuntime targetFramework="4.7.2" />
在我的应用程序中,我从另一个 WebAPI 方法(理想情况下是内部服务调用)调用用 WebAPI 编写并托管在 Azure 的 PaaS 环境中的 API,假设 WebApp_A 中的 MethodA 正在调用 WebApp_B 中的 MethodB。但是如果 WebApp_B 的 TLS 设置是 1.1 或 1.2(适用于 1.0),我会收到上述错误。 "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
我有类似的 WebApp (WebApp_C),它没有 TLS 错误。下面是我们用来从 WebApp_A
调用 WebApp_B 中的 MethodB 的代码 public async Task<ServiceResponse> CreateDialog(RequestObject requestObject)
{
ServiceResponse serviceResponse = new ServiceResponse();
try
{
using (var client = new HttpClient())
{
SessionTokenBo jwtData = new SessionTokenBo();
Logger = _logger;
jwtData = GetInternalToken(InternalCallTypes.Utilities.ToString(), int.Parse(ServiceConfiguration.TokenExpiryWindow), int.Parse(ServiceConfiguration.InternalTokenExpiryWindow));
if (null != jwtData)
{
client.BaseAddress = new Uri("URI");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtData.Token); HttpResponseMessage response = await client.PostAsJsonAsync("service/methodB", requestObject);
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync().Result;
serviceResponse = JsonConvert.DeserializeObject<ServiceResponse>(data);
}
}
}
}
catch (Exception ex)
{
throw
}
return serviceResponse;
}
如果我提供这样的安全协议,它将起作用
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
我也尝试了邮递员的请求,但没有失败。所以现在很困惑,因为如果邮递员的工作正常,那么理想情况下它不是 WebApp 设置问题
TLS 1.0 不再是默认设置。在发出请求之前添加此行:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
参考这个issue。
更新:
but my doubt is why the same code is not failing for other web apps ?
发现问题,是因为 webconfig 中的目标框架是 4.5.2。
<compilation targetFramework="4.7.2"></compilation>
<httpRuntime targetFramework="4.7.2" />