不同路径的 Owin AuthenticationMode Active 和 Passive

Owin AuthenticationMode Active and Passive for different path

我正在使用 Owin WsFederation 身份验证。对于未经授权的用户,我希望将一条路径重定向到 STS,将另一条路径重定向到 return 401 响应。是否可以为不同的路径设置不同的AuthenticationMode?

据我所知 - 但您可以在管道中添加多个中间件实例,使用不同的 AuthenticationType,并在不同的路由上使用它们。

您可以 "fork" OWIN 管道,以便为不同的路径配置不同的中间件。

public void Configuration(IAppBuilder app)
{
    app.UseErrorPage(new ErrorPageOptions());

    app.Map("active", active => 
    {
        active.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active
                //TODO: Add other options.
            });
    });

    app.Map("passive", passive =>
    {
        passive.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Passive,
                //TODO: Add other options.
            });
    });
}

此示例针对所有请求将 "error page" 中间件配置为 运行,然后继续创建两个 URL 映射管道,一个用于开始 /active 的请求另一个用于开始 /passive 的路径。这些映射管道中的每一个都部分配置了一个 OIDC 身份验证中间件,分别使用主动和被动模式。

此机制旨在让您 运行 在特定路径上收到请求时使用不同的中间件集。将通用中间件放在映射的中间件之前,使其在每个请求上都 运行。