gpt4 book ai didi

c# - 尝试使用 HttpClient 检索访问 token 时出现错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:24 25 4
gpt4 key购买 nike

尝试使用 HttpClient 从 Windows 服务器检索访问 token 时出现错误:

"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 步。根据 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

来解决此问题

关于c# - 尝试使用 HttpClient 检索访问 token 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57791742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com