asp.net-core - 为什么 `UseAuthentication` 必须放在 `UseRouting` 之后而不是之前?

标签 asp.net-core asp.net-core-3.0 asp.net-core-middleware

根据documentation ,中间件的顺序应该是这样的:

app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

我有中间件来保护静态文件,基于 this article (保护某些路线)。我遇到的问题是订单对我不起作用。如果用户已获得授权,我只能保护文件夹。所以我需要放置 UseProtectFolder之前 UseStaticFiles之后 UseAuthenticationUseAuthorization :
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseProtectFolder(new ProtectFolderOptions
{
    Path = "/Secret",
    PolicyName = "Authenticated"
});
app.UseStaticFiles();

但这不会返回任何静态文件。它看起来像 UseRouting正在做一些使文件不可用的事情,返回 404,因为当我将顺序更改为此时,移动了 UseRouting之后 UseStaticFiles , 有用:
app.UseAuthentication();
app.UseAuthorization();

app.UseProtectFolder(new ProtectFolderOptions
{
    Path = "/Secret",
    PolicyName = "Authenticated"
});
app.UseStaticFiles();

app.UseRouting();

所以实际的顺序变化是UseAuthentication放在 UseRouting 之前(甚至在 UseStaticFiles 之前)。

从文档:

The order that middleware components are added in the Startup.Configure method defines the order in which the middleware components are invoked on requests and the reverse order for the response. The order is critical for security, performance, and functionality.



我现在的问题是:按照记录的顺序,为什么 UseAuthentication置于 UseRouting 之后?

是否有特殊原因还是仅出于性能原因?并且通过在管道中更早地移动身份验证/授权,这是否会影响响应(相反的顺序)?

最佳答案

发布这个问题后,我打开了一个关于 routing 的问题。在 github 上,还有一个关于 localization ,希望得到更多信息。尽管并非所有问题都以直接的方式回答,但它帮助我找到了这个问题的答案。

看完comment of David Fowler :

UseAuthorization() -> Will look at the populated user and the current endpoint to determine if an authorization policy needs to be applied.



我突然想到 UseAuthorization 没有问题。它用于端点,所以我不需要它来保护文件夹。它还解释了为什么此语句仅在 UseEndpoints 语句之后才有意义。

为了全面了解我的配置,我有一个策略提供程序(包括策略)、一个 url 重写器(如 UseDefaultFiles)和保护某些文件夹的中间件。

我的结论是,我可以使用以下顺序,这与记录的几乎相同:
// Identify the user. The only statement that is not in the order as documented
app.UseAuthentication();

// Middleware that adds policies
app.UsePolicyProvider();
// Protect the folder by policy
app.UseProtectFolder(new ProtectFolderOptions { Path = "/p", PolicyName = "admin" });
// URL rewriter for serving tenant specific files
app.UseTenantStaticFiles();

// Serve the static files
app.UseStaticFiles();

app.UseCookiePolicy();
app.UseCors();

app.UseRouting();
app.UseRequestLocalization();
app.UseAuthorization();
app.UseEndpoints();

关于订单的两点说明:
  • UseRequestLocalization 仅在 UseRouting 之后有效
  • 当涉及到 URL 重写器(如 UseDefaultFiles)时,UseStaticFiles 在 UseRouting 之后不起作用。
  • 关于asp.net-core - 为什么 `UseAuthentication` 必须放在 `UseRouting` 之后而不是之前?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58475596/

    相关文章:

    cookies - ASP.NET Core MVC (.NET Framework) 编写、读取和更新 cookie

    visual-studio - .NET 核心 3.0 : ASPNETCORE_ENVIRONMENT has value Development on publish

    c# - 没有注册类型 'Microsoft.Framework.Runtime.ILibraryManager' 的服务

    validation - 使用 ASP.NET 5 禁用客户端验证

    azure-devops - Azure DevOps : Pipeline broken after migrating to . NET 核心 3.0

    c# - “不支持返回 System.IServiceProvider 的 ConfigureServices。”

    c# - ASP.NET Core 2.x 中间件的自定义依赖注入(inject)解析器?

    c# - EntityFrameworkCore RoleManager FindByIdAsync 可以填充声明吗?

    c# - 有没有办法将元数据添加到 ASP.NET Core 中的所有 GET 端点?

    visual-studio - 成功发布后,Asp.net 5 Web 应用程序启动异常