gpt4 book ai didi

c# - 如何为 Kestrel 主机添加 NTLM 支持?

转载 作者:行者123 更新时间:2023-12-04 15:39:37 25 4
gpt4 key购买 nike

我们想使用 Kestrel 来托管我们的 web-api。我们必须同时支持 NTLM 和协商身份验证。

Core 3.0 应该可以做到 https://learn.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-3.0&tabs=visual-studio

然而,当 Kestrel 响应挑战时,仅返回协商方案。有没有人设法使用 Kestrel 实现 NTLM 身份验证?

应用程序在 Windows 10 机器上运行

基本上我们都遵循了建议。首先向服务添加身份验证:

        services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();

然后向管道添加身份验证

        app.UseAuthentication();

我们还在筹备中拥有自己的中间件,以确保用户已通过验证

        app.UseMiddleware<ValidateAuthentication>();

实现是这样的

internal class ValidateAuthentication : IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
if (context.User.Identity.IsAuthenticated)
await next(context);
else
await context.ChallengeAsync();
}
}

问题是challange响应只有Negotiate

    WWW-Authenticate Negotiate

我希望 NTLM 和协商

    WWW-Authenticate NTLM, Negotiate

最佳答案

您可以覆盖 HandleChallengeAsync方法,然后替换处理程序:

public sealed class NtlmNegotiateHandler : NegotiateHandler
{
public NtlmNegotiateHandler(
IOptionsMonitor<NegotiateOptions> options,
ILoggerFactory logger, UrlEncoder encoder,
ISystemClock clock) : base(options, logger, encoder, clock)
{
}

protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
{
await base.HandleChallengeAsync(properties);

if (Response.StatusCode == StatusCodes.Status401Unauthorized)
{
Response.Headers.Append(Microsoft.Net.Http.Headers.HeaderNames.WWWAuthenticate, "NTLM");
}
}
}
public sealed class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();

// replace the handler
var serviceDescriptor = new ServiceDescriptor(typeof(NegotiateHandler),
typeof(NtlmNegotiateHandler),
ServiceLifetime.Transient);

services.Replace(serviceDescriptor);
}
}

关于c# - 如何为 Kestrel 主机添加 NTLM 支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58318783/

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