将旧 Web 窗体移植到 OWIN 时获取 HTTP 401.2 未经授权

Getting HTTP 401.2 Unauthorized when porting old Web Forms to OWIN

我关注了 Require Authentication for all requests to an OWIN application 所以我的代码如下所示:

public void Configuration(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    app.UseCookieAuthentication(new CookieAuthenticationOptions() { CookieSecure = CookieSecureOption.Never });
    app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions()
    {
        MetadataAddress = "https://login.windows.net/#####.onmicrosoft.com/federationmetadata/2007-06/federationmetadata.xml",
        Wtrealm = "http://localhost:33210/",
        // SignInAsAuthenticationType = // This is picked up automatically from the default set above
    });


    app.Use(async (context, next) =>
    {
        var user = context.Authentication.User;
        if (user == null || user.Identity == null || !user.Identity.IsAuthenticated)
        {
            context.Authentication.Challenge();
            return;
        }
        await next();
    });
}

该应用程序是 Web 窗体和 MVC 的混合体。我已经从 IIS 中删除了所有身份验证类型,删除了 authorizationmembershiproleManager 并在 web.config.

中设置了 authentication mode="None"

现在,当我通过 Default.aspx、[Authorize] 控制器或强制质询的显式 AccountController 访问站点时:

public void SignIn()
{
    if (!Request.IsAuthenticated)
    {
        HttpContext.GetOwinContext().Authentication.Challenge(
            new AuthenticationProperties { RedirectUri = "/" },
            WsFederationAuthenticationDefaults.AuthenticationType);
    }
}

所有结果都会导致 HTTP 错误 401.2 由于身份验证无效,您无权查看此页面 headers。我错过了什么?

问题是

I've removed all authentication types from IIS

至少需要在 IIS 中选择 Anonymous Authentication。通过在 IIS Express 的项目属性中禁用 Windows 和匿名身份验证,使用 WebApp-OpenIDConnect-DotNet 等示例应用程序可以重现相同的错误。