【问题标题】:Intercept Unauthenticated request with Windows Authentication使用 Windows 身份验证拦截未经身份验证的请求
【发布时间】:2021-07-05 10:51:34
【问题描述】:

我有一个启用了 Windows 身份验证的 ASP.NET Core MVC 应用程序,我想使用一个基本的拦截器来处理未经身份验证的请求,但它完全被跳过了,因为 Windows 身份验证机制在进入拦截器之前阻止了请求。

您知道是否有一种方法可以绕过这种标准行为而不阻止进入拦截器管道的请求?

提前致谢!

【问题讨论】:

  • 一种解决方法是在 IIS 上禁用 Windows 身份验证,但禁用匿名身份验证。然后您可以从 ASP.NET Core 端发送 401.2 质询响应来模拟 Windows 身份验证。但是为什么要在这样的设置中处理未经身份验证的请求呢?您可能会为这些目的找到其他方法。
  • @LexLi 的目的是绕过某些动态控制器的 Windows 身份验证,这些控制器由于不驻留在项目中而是来自外部 nuget 而失控。

标签: asp.net-core iis windows-authentication asp.net-core-3.1


【解决方案1】:

使用中间件并在 UseAuthorization 之前声明它

public class YourMiddleware
{
    private readonly RequestDelegate _next;

    public YourMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        Console.WriteLine("Before");
// Here you can decide if you want the next step of the pipeline or not, usually you want
        await _next(context);
    }
}
app.UseMiddleware<YourMiddleware>();
app.UseAuthorization();

【讨论】:

  • 您好 Dario,感谢您的回复,我已经尝试过此解决方案,但它不起作用。似乎 WindowsAuthentication 功能在 Middelware 管道中仍然具有更高的优先级。
  • 挑战响应 (401.2) 是由 Windows 身份验证模块生成的,该模块在 IIS 处理管道中的 ASP.NET Core 模块之前执行很长时间,因此任何像上面这样的拦截尝试都是无用的。
  • 你是对的。但在过去的 5 年里,我没有在 IIS 上运行过应用程序,所以我完全没有想到 Windows 环境。我制作了在红隼上运行的示例并且可以正常工作。但你是对的:如果 kestrel 在 iis 后面,这不是一个正确的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-12
  • 2019-09-25
  • 1970-01-01
  • 2011-05-18
  • 2020-06-07
  • 1970-01-01
相关资源
最近更新 更多