1

I have an ASP.NET Core 5 MVC app, with the default/root route set like this inside PageController:

[AllowAnonymous]
[Route("/")]
public IActionResult __Home(int? parent)
{
    return View();
}

This worked fine until I added OpenIdConnect authentication. After that, the root (/) page no longer routes to __Home in the PageController, it just returns a blank page. All other pages route just fine.

When I comment out this:

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApp(Configuration, "AzureAdB2C");

then / works again, so I know it's something to do with the authentication. As you can see, I have added [AllowAnonymous] to that action.

I have this in my startup:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}"
    );
});

Any ideas on how to fix this? I know it's unconventional to have the default/root route in a weird controller/action like that, but there are reasons for it, so I'm hoping it can still work.

More Info:

I found that if I move app.UseEndpoints above app.UseAuthentication, then the home page shows. After logging in (with B2C), however, it goes into an infinite loop (i.e. the authentication token doesn't stick?).

EDIT: My Startup.cs class

using Blank.Models;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Identity.Web;

namespace Blank
{
    public class Startup
    {
        private readonly AppSettings appSettings = null;

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            this.appSettings = new AppSettings();
            this.Configuration.Bind(this.appSettings);
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            
            services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApp(Configuration, "AzureAdB2C");

            services.AddSession();

            services.Configure<OpenIdConnectOptions>(Configuration.GetSection("AzureAdB2C"));

            services.AddControllersWithViews(options =>
            {
                var policy = new AuthorizationPolicyBuilder()
                    .RequireAuthenticatedUser()
                    .Build();
                options.Filters.Add(new AuthorizeFilter(policy));
            });


            services.Configure<AppSettings>(this.Configuration);

            services.AddEntityFrameworkSqlServer().AddDbContext<BlankDBContext>(
               Options => Options.UseSqlServer(Microsoft.Extensions.Configuration.ConfigurationExtensions.GetConnectionString(this.Configuration, "BlankDatabase"))
                );
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();


            app.UseSession();

            app.UseAuthentication();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Page}/{action=Index}/{id?}");
            });

        }
    }

}

Edit 2

I think that app.UseAuthentication() is breaking/returning the blank page, because when I put the following code before app.UseAuthentication() I get something on the home page, and if it's after then blank:

app.Use(async (context, next) =>
{
    var endpoint = context.GetEndpoint();

    if (endpoint != null)
    {
        await context.Response.WriteAsync("<html> Endpoint :" + endpoint.DisplayName + " <br>");

        if (endpoint is RouteEndpoint routeEndpoint)
        {
            await context.Response.WriteAsync("RoutePattern :" + routeEndpoint.RoutePattern.RawText + " <br>");
        }

    }
    else
    {
        await context.Response.WriteAsync("End point is null");
    }

    await context.Response.WriteAsync("</html>");
    await next();
});

So perhaps it has to do with my authentication? Here's my appsettings.json:

"AzureAdB2C": {
    "Instance": "https://abc.b2clogin.com",
    "Domain": "abc.onmicrosoft.com",
    "ClientId": "62...f1",
    "TenantId": "7e...ae",
    "SignUpSignInPolicyId": "B2C_1_SUSI",
    "SignedOutCallbackPath": "/"
},
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
MG123
  • 402
  • 2
  • 14
  • What did you set up as the `CallbackPath`? – abdusco Aug 14 '21 at 10:05
  • I've added the entire startup class ... regarding the CallbackPath, I have / and /signin-oidc setup in B2C – MG123 Aug 14 '21 at 11:55
  • I'm asking about the callback path, because you used `Configuration` to configure the auth scheme `.AddMicrosoftIdentityWebApp(Configuration, ...)`. This means the values are actually in `appsettings.json` (or any other source). Regardless, if you've set up the callback path wrong (or it conflicts with an endpoint), `AuthenticationMiddleware` will intercept the request and the request will never reach to the controller. That's why you shouldn't set `/` as the callback path. – abdusco Aug 14 '21 at 12:04
  • Ahh ok, sorry I meant that redirect url is set to / and /signin-oidc in the B2C app registration. My appsettings,json only has SignedOutCallbackPath: "/" in addition to Instance, Domain, ClientId, TenantId and SignUpSignInPolicyId – MG123 Aug 14 '21 at 12:13

1 Answers1

1

Turns out the problem was this in my appsettings.json:

"SignedOutCallbackPath": "/"

Removing this fixed the problem, and the home page now loads correctly.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
MG123
  • 402
  • 2
  • 14
  • Thanks for this. I also encountered this problem and removing this fixed the problem, but can you explain why setting this config to "/" causes the problem? – Harry Jul 07 '23 at 06:27
  • no idea sorry Harry – MG123 Jul 12 '23 at 02:18