Reputation: 703
In my application I am calling an API written in WebAPI and hosted in a PaaS environment in azure from another WebAPI method (ideally an internal service call), say MethodA in WebApp_A is calling MethodB in WebApp_B. But i am getting the mentioned error if the TLS settings of WebApp_B is either 1.1 or 1.2 ( it works with 1.0). "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
I have similar WebApp (WebApp_C) which doesn't have the error with TLS. Below is the code that we use to call the MethodB in WebApp_B from WebApp_A
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;
}
If i give Security protocol like this , it will work
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Also i tried from postman with the request and it is not failing. So now am confused because if from postman its working then ideally its not the WebApp setup issue
Upvotes: 0
Views: 501
Reputation: 20067
TLS 1.0 is no longer the default. Add this line before making the request:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Have a Reference to this issue.
Update:
but my doubt is why the same code is not failing for other web apps ?
Found the issue, it was due the target framework was 4.5.2 in webconfig.
<compilation targetFramework="4.7.2"></compilation>
<httpRuntime targetFramework="4.7.2" />
Upvotes: 1