0

I have a code for getting token. It works in live server. But when I am tracing the code in my system I get this error:

The request was aborted: Could not create SSL/TLS secure channel

When I try to get token by Postman on my system, no error occurs. So why does the error occur? It is driving me crazy so I would appreciate any help.

Here is my code:

var clientKey = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("client_id", XXXX),
                new KeyValuePair<string, string>("client_secret", YYY),
                new KeyValuePair<string, string>("grant_type", "client_credentials")
            });

        clientKey.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("client_id", XXXX));
        clientKey.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("client_secret", YYY));
        clientKey.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("grant_type", "client_credentials"));
        
        var url = TokenUrl;
        var client = new HttpClient();

        client.DefaultRequestHeaders.Add("Cookie", "cookiesession1=xxxxxxxxxxxxxx");

        var httpResponse = client.PostAsync(url, clientKey).Result;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    Which version of Windows and .Net (exact build numbers please)? What TLS version does the URL support? Is it a public URL, if so can you supply at least the hostname `https://somesite.com` Side notes: `client` and `httpResponse` need disposing with `using`, and do not use `.Result` as it can cause deadlocks, instead use `await` – Charlieface May 31 '23 at 13:40
  • @Charlieface Win 10 and .Net 4.8.09037. I dont know anything about service provider. I just use their service and I am not aware of details. – Curious Developer Jun 20 '23 at 11:03
  • Can you please supply the `url` you are using? – Charlieface Jun 20 '23 at 11:08

1 Answers1

0

Finally I found a solution by adding these codes to my source. Since in live server there is no need to this code, I put this on a block which only works if it is test server:

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;