尝试使用HttpClient检索AccesToken时出现错误

时间:2019-09-04 15:38:09

标签: c# .net linux .net-core dotnet-httpclient

尝试使用HttpClient从Windows服务器检索Acces令牌时,出现错误:

  

“ GSSAPI操作失败,并显示错误-提供了无效的状态代码(SPNEGO无法找到进行协商的机制)。”

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由于GSSAPI操作失败并出现错误而引发-提供了无效的状态代码(SPNEGO无法找到进行协商的机制)

上面的代码在Windows中工作正常,但在Linux中工作不正常(我正在使用Linux Mint)。据我所知,它是指尝试使用Kerberos的问题,但是没有Kerberos票证可用于Linux身份验证。

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决这个问题的方法。

解决方案1:

步骤1。根据this,您应该添加

 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

来解决此问题。
相关问题