尝试使用 HttpClient 检索访问令牌时出现错误

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

尝试使用 HttpClient 从 windows 服务器检索访问令牌时出现错误:

"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)

以上代码在 windows 中运行良好,但在 Linux 中运行不正常(我使用的是 Linux Mint)。据我所知,它指的是尝试使用 Kerberos 但没有激活 Kerberos 票证来对 Linux.

进行身份验证的问题

终于,我找到了解决这个问题的方法。

解决方案 1:

第一步。根据,你应该添加

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

第 2 步。由于 UseDefaultCredentials = true 在这里不起作用,您需要将网络凭据手动传递给 HttpClient,如下所示

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

第 3 步。您应该将 HttpRequestMessage 版本更改为

HttpVersion.Version11.

解决方案 2:您也可以通过将整个应用程序转换为 NetcoreApp3.0

来解决此问题