Search code examples
c#wpfsslwebservice-client

Could not establish secure channel for SSL/TLS C# Web service Client


I have a WPF application and that calling 4 web services (Written with Java) on the same base URL and it was working perfectly until I install google chrome. I installed chrome and I've got this error:

Could not establish secure channel for SSL/TLS C# Web service

I didn't write another code. That happened for just I installed chrome then I remove chrome but didn't work and I tried system restore, uninstall eset smart security and I cleaned all windows(8.1 single languages) certificate. So how can I figure it out? Here is my web service caller

public string call(string url, string json)
{
    try
    {
        var webrequest = (HttpWebRequest)WebRequest.Create(url);
        var key = JsonConvert.SerializeObject(LoginService.SessionData.SessionKey);
        UTF8Encoding uTF8Encoding = new UTF8Encoding();
        byte[] requestBytes = uTF8Encoding.GetBytes(json);
        WebClient client = new WebClient();

        webrequest.Method = "POST";
        webrequest.Headers.Add("SESSION_KEY", LoginService.SessionData.SessionKey);
        webrequest.ContentType = "application/json";
        webrequest.ContentLength = requestBytes.LongLength;
        Stream requestStream = webrequest.GetRequestStream();//here the exception
        requestStream.Write(requestBytes, 0, requestBytes.Length);

        using (var response = webrequest.GetResponse())
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            var responseBuf = reader.ReadToEnd();
            String responseJson = Convert.ToString(responseBuf);
            return responseJson;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return null;
}

Solution

  • By my observation there are a large number of services over the last several months that are turning off SSL and/or older TLS versions to mitigate security problems inherent in them.

    Anywhere within the AppDomain you can just force the connection to use TLS 1.2 like this:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

    You can also OR together multiple versions if you need to support older ones as well:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12