OIDC infinite redirect loop .NET Core 3.1

Viewed 964

Encountering Infinite Redirect Loop after ADFS authentication. This is my configuration. Deployed on AWS EC2 IIS behind a Application Load Balancer.

Loops redirecting on "/signin-oidc"

Based on dev tools request happens on an infinite loop until Bad Request too long result.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
        services.AddScoped<HttpClient>();
        services.Configure<ForwardedHeadersOptions>(o =>
        {
            o.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            o.KnownNetworks.Clear();
            o.KnownProxies.Clear();
        });

        services.AddAuthentication(o =>
        {
            o.DefaultScheme = "Cookies";
            o.DefaultChallengeScheme = "oidc";
        })
       .AddCookie("Cookies")
       .AddOpenIdConnect("oidc", options =>
       {
               options.Authority = "https://testadfs.com/adfs";
               options.ClientId = "clientid";
               options.ClientSecret = "clientsecret";
               options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
               options.CallbackPath = "/signin-oidc";
               options.SaveTokens = true;
               options.GetClaimsFromUserInfoEndpoint = true;
           options.Events = new OpenIdConnectEvents
           {
               OnAccessDenied = context =>
               {
                   context.HandleResponse();
                   context.Response.Redirect("/Error/CustomInternalServerError");
                   return Task.CompletedTask;
               }
           };
        }
        services.AddMvcCore(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        });

        services.AddAuthorization();

}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseForwardedHeaders();
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error/CustomInternalServerError");
            app.UseHsts();
        }

        app.UseCors("AllowOrigin");
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseCookiePolicy();
        app.UseAuthorization();
        app.UseAuthentication();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
        });

    }
1 Answers

app.UseAuthorization() should be executed after app.UseAuthentication()

Related