Skip to content
Advertisement

While trying to retrieve the AccesToken using HttpClient I am getting an error

While trying to retrieve the Acces Token from a windows server using HttpClient I am getting an error:

“GSSAPI operation failed with error – An invalid status code was supplied (SPNEGO cannot find mechanisms to negotiate).”

private readonly HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true, AllowAutoRedirect = true }) { Timeout = TimeSpan.FromSeconds(5) };



public async Task<UserAccessToken> GetAuthenticationToken(string accessBrokerHost)
    {
        try
        {
            var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, $"{accessBrokerHost}/Token")).ConfigureAwait(false); 


//accessBrokerHost is HTTP SPN created internally in a windows server


            if (!response.IsSuccessStatusCode)
            {                    
                throw new BrokerNotAvailableException();
            }
            return await response.Content.ReadAsAsync<UserAccessToken>().ConfigureAwait(false);
        }            
    }

system.ComponentModel.win32Exception is throwing as GSSAPI operation failed with error – An invalid status code was supplied (SPNEGO cannot find mechanisms to negotiate)

The above code is working fine in windows but not in Linux (I am using Linux Mint). As of my knowledge, it refers to a problem trying to use Kerberos but no Kerberos ticket is active to authenticate for Linux.

Advertisement

Answer

Finally, I found a solution to this question.

Solution 1:

Step 1. According to this, you should add

 AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);

Step 2. Since UseDefaultCredentials = true won’t work here, you need to pass your Network credentials manually to HttpClient like below

HttpClient client = new HttpClient(new HttpClientHandler{Credentials = new NetworkCredential("UserName" "Password", "Domain")}

Step 3. You should change your HttpRequestMessage version to

HttpVersion.Version11.

Solution 2: You can also fix this issue by converting your entire application into NetcoreApp3.0

Advertisement