UseOpenIdConnectAuthentication 中间件在自托管时不起作用

UseOpenIdConnectAuthentication middleware not working when self hosted

我有一个托管在 IIS 上的 WebAPI,我正在尝试将其移至 Service Fabric。一切正常,除了我的 OpenIdConnect 中间件。重定向到身份验证提供程序永远不会发生。我将其设置为仅在用户使用 MapWhen

访问某些路线时重定向用户
 app.MapWhen(owinContext =>
        {
            if (owinContext.Request.Path.Value.ToLower().StartsWith("/someroute))
            {
                return true;
            }
            return false;
        }, adApp => adApp.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions

我可以调试到 MapWhen,我看到它返回 true,但没有重定向,什么也没有发生。任何想法可能导致重定向失败?此中间件是否与自托管应用程序兼容?

如果您正在做一个新项目,您应该避免使用 UseOpenIdConnectAuthentication 并改用 AddAuthentication().AddOpenIdConnect。

你可以看到像 here:

这样的提及

[Obsolete("UseOpenIdConnectAuthentication 已过时。在 ConfigureServices 中使用 AddAuthentication().AddOpenIdConnect 配置 OpenIdConnect 身份验证。有关详细信息,请参阅 https://go.microsoft.com/fwlink/?linkid=845470。 ", 错误: 真)]

此外,我怀疑你是否应该将它放在 MapWhen 中。

我最终删除了中间件并使用 Thinktecture.IdentityModel.Client.OAuth2Client 来启动重定向服务器端。

OpenIdConnect Owin 中间件旨在用于保护 MVC 页面。当我从我的应用程序中删除 MVC 路由以进行自我托管时,我相信这就是破坏它的原因。