.net核心spnego身份验证

pdsfdshx  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(387)

我目前正在编写一个简单的基于.net内核的客户端,用于通过webhcat与hadoop集群进行交互,我正在尝试找出如何使用spnego进行身份验证,就像在curl或powershell内核中一样。
使用curl,我可以查询webhcat的状态端点,如下所示:

curl "http://10.2.0.9:50111/templeton/v1/status" --negotiate -k -u :

同样的请求也可以在powershell核心中执行:

$client = New-Object System.Net.WebClient;
$client.UseDefaultCredentials = $true;
$client.DownloadString("http://10.2.0.9:50111/templeton/v1/status");

但是,在与群集位于同一服务器上的.net核心项目中运行以下内容时:

using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace testauth
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var host = "http://10.2.0.9:50111/templeton/v1/status";
            var handler = new HttpClientHandler
            {
                UseDefaultCredentials = true,
                AllowAutoRedirect = true,
            };
            using (var client = new HttpClient(new LoggingHandler(handler)))
            {
                var res = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, host));
            }
        }
    }

}

我得到以下错误:

Unhandled Exception: System.ComponentModel.Win32Exception: GSSAPI operation failed with error - An invalid status code was supplied (Server not found in Kerberos database).
   at System.Net.NTAuthentication.GetOutgoingBlob(Byte[] incomingBlob, Boolean throwOnError, SecurityStatusPal& statusCode)

客户端正在运行.NETCore2.1,所以正如本期中提到的那样,我应该能够将默认凭据传递到处理程序中,它将按预期工作。
我也尝试过编写与c中powershell内核相同的代码,尽管代码相同,但仍然抛出相同的错误?
我唯一能给出的其他细节是,kerberos连接到只有一个用户的activedirectory示例,并且所有这些请求都是在用创建了该用户的票证之后运行的 kinit .

zu0ti5jz

zu0ti5jz1#

我设法找到了解决办法。使用asp.net core 2.1,他们引入了一个新的 SocketsHttpHandler 默认情况下用于请求。这意味着在某些平台上,它可能会覆盖 HttpHandler 在我的请求中提供,因此默认为您应该使用的套接字处理程序:

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

这解决了我的请求。

相关问题