简体   繁体   English

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

[英]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: 尝试使用HttpClient从Windows服务器检索Acces令牌时,出现错误:

"GSSAPI operation failed with error - An invalid status code was supplied (SPNEGO cannot find mechanisms to negotiate)." “ 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 is throwing as GSSAPI operation failed with error - An invalid status code was supplied (SPNEGO cannot find mechanisms to negotiate) 由于GSSAPI操作失败并出现错误,引发了system.ComponentModel.win32Exception-提供了无效的状态码(SPNEGO无法找到进行协商的机制)

The above code is working fine in windows but not in Linux (I am using Linux Mint). 上面的代码在Windows中工作正常,但在Linux中工作不正常(我正在使用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. 据我所知,它是指尝试使用Kerberos的问题,但没有Kerberos票证可用于Linux身份验证。

Finally, I found a solution to this question. 最后,我找到了解决这个问题的方法。

Solution 1: 解决方案1:

Step 1. According to this , you should add 步骤1.根据 ,您应该添加

 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 第2步。由于UseDefaultCredentials = true在这里不起作用,因此您需要将网络凭据手动传递给HttpClient,如下所示

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

Step 3. You should change your HttpRequestMessage version to 步骤3.您应该将HttpRequestMessage版本更改为

HttpVersion.Version11 . HttpVersion.Version11

Solution 2: You can also fix this issue by converting your entire application into NetcoreApp3.0 解决方案2:您还可以通过将整个应用程序转换为NetcoreApp3.0来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM