ASP.NET Core 3.0 无限重定向循环

ASP.NET Core 3.0 in endless redirection loop

我最近将现有的 ASP.NET Core 2.2 网络应用程序升级到了 3.0。一切编译。我们使用 Azure ADB2C 进行身份验证。以前,用户将被定向到登录页面,在输入他们的凭据后,他们将被导航到默认页面。

升级到 ASP.NET Core 3.0 后,在将他们定向到登录页面后,应用程序进入无限重定向循环。

我可以使用旧版本的应用程序(仍然使用 ASP.NET Core 2.2)正确登录,所以它不是 Azure ADB2C 配置。一定是 routingASP.NET Core 3.0 应用程序中的一些错误配置。

这是我的启动配置。

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(option => option.EnableEndpointRouting = false);

    services.AddAuthentication(sharedOptions =>
    {
        sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
    .AddAzureAdB2C(options => Configuration.Bind("AzureAdB2C", options))

    //ensure the user must be authenticated before they can access any of the pages from the app
    services.AddMvc()
        .AddRazorPagesOptions(options => { options.Conventions.AuthorizeFolder("/"); });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseMvcWithDefaultRoute();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    { 
        app.UseExceptionHandler("/Error");
    }

    app.UseAuthentication();
}

此代码的大部分保持不变,并且在 ASP.NET Core 2.2 下 运行 时可以正常工作。由于我已经升级到 ASP.NET Core 3.0,它进入了无限重定向循环。

我看不出是什么导致了这个问题。

你的中间件顺序不对,app.UseMvcWithDefaultRoute();需要放在app.UseAuthentication();

之后
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   //...
   app.UseAuthentication();
   app.UseMvcWithDefaultRoute();
}