asp.net-coreazure-ad-b2cblazor-server-sideasp.net-core-5.0

How to disable authentication during the start up of an ASP.NET Core 5 Blazor B2C application?


I created an ASP.NET Core 5 Blazor Server App with a B2C Login. The following is the start-up code. I see a B2C login page coming up when I run the application. How can I prevent the authentication during the startup? I want the user to login by clicking the Login link only from the top menu.

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
            .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAdB2C"));

        services.AddControllersWithViews()
            .AddMicrosoftIdentityUI();

        services.AddAuthorization(options =>
        {
            // By default, all incoming requests will be authorized according to the default policy
            options.FallbackPolicy = options.DefaultPolicy;
        });

        services.AddRazorPages();
        services.AddServerSideBlazor()
            .AddMicrosoftIdentityConsentHandler();

        services.AddSingleton<WeatherForecastService>();
    }

    // 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("/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.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapBlazorHub();
            endpoints.MapFallbackToPage("/_Host");
        });
    }
}

Solution

  • I tested your code and it looks like options.FallbackPolicy = options.DefaultPolicy; is causing the automated redirect. Comment that out and give it a try, it should work. I also created a ASP.NET Core 5 Blazor Server app and tested this piece of code. After commenting the line mentioned above, I do get the Login button available for the user to click on it.

    services.AddControllersWithViews()
            .AddMicrosoftIdentityUI();
    
    services.AddAuthorization(options =>
    {
        // By default, all incoming requests will be authorized according to the default policy
        //options.FallbackPolicy = options.DefaultPolicy;
    });