Application crashes when Http POST is made

0

I'm writing small utiliy for making HTTP calls. Server uses self-signed SSL certificate. Problem is: when I try to post somethint to the server application crashes without excpetion, console retuns exit status 0. Executing HTTP code directly from browser works and I get response back.

I have already looked on stackoverflow and there were ideas to add Security protocol types to the ServicePointManager.SecurityProtocol. I tried most of proposed solutions but I have stuck :)

Here is my code:

    private async Task<T> PostAsync<T>(string address, string payload)
    {
        var endpointAddress = address;

        var handler = new HttpClientHandler();

        if (!_certificateSigned)
        {
            handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
        }

        using (var httpClient = new HttpClient(handler))
        {
            ServicePointManager.SecurityProtocol =
                SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            try
            {
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeader;
                var json = new StringContent(payload, Encoding.UTF8, "application/json");

                var response = await httpClient.PostAsJsonAsync(address, json);
                response.EnsureSuccessStatusCode();

                if (response.IsSuccessStatusCode)
                {
                    var responseData = await response.Content.ReadAsStringAsync();
                    return JsonConvert.DeserializeObject<T>(responseData);
                }
            }
            catch (Exception ex)
            {

            }
        }

        return default(T);
    }

In short I'm trying to rewrite this curl API call:

curl -u "${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}" \
-X POST \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
'https://192.168.5.30:444/v3/clusterregistrationtokens'

interesting thing is that GetAsync method works just fine. Code for get async method

    private async Task<T> GetAsync<T>(string address)
    {
        var endpointAddress = address;

        var handler = new HttpClientHandler();
        using (var httpClient = new HttpClient(handler))
        {
            if (!_certificateSigned)
            {
                handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
            }

            try
            {
                httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeader;
                var response = await httpClient.GetAsync(endpointAddress);

                if (response.IsSuccessStatusCode)
                {
                    var responseData = await response.Content.ReadAsStringAsync();
                    return JsonConvert.DeserializeObject<T>(responseData);
                }
            }
            catch (Exception ex)
            {

            }
        }

        return default(T);
    }

And this is my authorization header value

private AuthenticationHeaderValue AuthenticationHeader => new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_accessKey}:{_secretKey}")));

Log from fiddler

[Fiddler] The connection to '192.168.5.30' failed. System.Security.SecurityException Failed to negotiate HTTPS connection with server.fiddler.network.https> HTTPS handshake to 192.168.5.30 (for #21) failed. System.Security.Authentication.AuthenticationException A call to SSPI failed, see inner exception. < The function requested is not supported

Win32 (SChannel) Native Error Code: 0x80090302

c#
visual-studio
asp.net-core

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0