0

Dotnet application is built with dotnet 4.5. On a few system, we are getting this error while connecting to the web server where few changes were made in SSL recently.

Web server TLS only 1.2 https://drive.google.com/open?id=1Z0S-MWugDZdQrIy3BnquooB0ZX7veIVT

Client side code.

WebRequest webRequest = WebRequest.Create(strUrl);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "Get";
if (strUrl.StartsWith("HTTPS", StringComparison.OrdinalIgnoreCase)) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
using (WebResponse webResponse = webRequest.GetResponse())
{
    if (webResponse == null) return blnResult;
    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
    {
       strSuccess = sr.ReadToEnd().Trim();
       blnResult = true;
    }
}

As soon as I compile it with framework 4.6.2 it works.

Why it is working on few system with same 4.5 compiled EXE and on few system it needs to be compiled with 4.6.2?

WebClient class works fine with 4.5 version of dotnet framework. Only WebRequest is not working.

Gentleman
  • 130
  • 1
  • 8
  • Read (carefully) the Remarks section of [ServicePointManager.SecurityProtocol](https://docs.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.securityprotocol). Carefully because in some *contexts* this description is considered controversial. Different *philosophies* may be in place and those depend on whom you ask or you're talking to. See the notes here: [Which TLS version was negotiated?](https://stackoverflow.com/a/48675492/7444103). These Docs: [Protocols in TLS/SSL (Schannel SSP)](https://docs.microsoft.com/en-us/windows/desktop/SecAuthN/protocols-in-tls-ssl--schannel-ssp-) – Jimi May 23 '19 at 12:56
  • Anyway, you didn't provide enough informations on the Systems where the handshake is failing. Also, the code that creates the connection is missing. – Jimi May 23 '19 at 12:59
  • Cilent side code added. – Gentleman May 24 '19 at 05:44
  • My bad, line WebRequest webRequest = WebRequest.Create(strUrl); should be used after setting SecurityProtocol. – Gentleman May 24 '19 at 05:54
  • If you solved it, post the code you used as the answer to the question. – Jimi May 24 '19 at 10:44

1 Answers1

0

Need to set security protocol first before initializing WebRequest object.

if (strUrl.StartsWith("HTTPS", StringComparison.OrdinalIgnoreCase)) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

WebRequest webRequest = WebRequest.Create(strUrl);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "Get";

using (WebResponse webResponse = webRequest.GetResponse())
{
    if (webResponse == null) return blnResult;
    using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
    {
       strSuccess = sr.ReadToEnd().Trim();
       blnResult = true;
    }
}
Gentleman
  • 130
  • 1
  • 8