Azure ad b2c 如何在Blazor B2C应用程序启动期间禁用身份验证?

Azure ad b2c 如何在Blazor B2C应用程序启动期间禁用身份验证?,azure-ad-b2c,blazor-server-side,asp.net5,Azure Ad B2c,Blazor Server Side,Asp.net5,我用B2C登录创建了一个ASP.NET 5 Blazor服务器应用程序。以下是启动代码。当我运行应用程序时,我看到一个B2C登录页面出现。如何在启动期间防止身份验证?我希望用户仅通过单击顶部菜单中的登录链接登录 { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfi

我用B2C登录创建了一个ASP.NET 5 Blazor服务器应用程序。以下是启动代码。当我运行应用程序时,我看到一个B2C登录页面出现。如何在启动期间防止身份验证?我希望用户仅通过单击顶部菜单中的登录链接登录

    {
        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");
            });
        }
    }
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection(“AzureAdB2C”);
services.AddControllersWithViews()
.AddMicrosoftIdentityUI();
services.AddAuthorization(选项=>
{
//默认情况下,将根据默认策略授权所有传入请求
options.FallbackPolicy=options.DefaultPolicy;
});
services.AddRazorPages();
services.AddServerSideBlazor()
.AddMicrosoftIdentityConsentHandler();
services.AddSingleton();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage(“/_主机”);
});
}
}

我测试了您的代码,它看起来像是
options.FallbackPolicy=options.DefaultPolicy导致自动重定向。评论出来并尝试一下,它应该会起作用。我还创建了一个ASP.NETCore5Blazor服务器应用程序,并测试了这段代码。在评论了上面提到的行之后,我确实获得了登录按钮供用户点击

services.AddControllersWithViews()
                .AddMicrosoftIdentityUI();

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

这个解决方案不起作用。该应用程序仍被重定向到B2C登录页面。请注意,我的应用程序是一个使用ASP.NET Core 5创建的Blazor服务器应用程序。更新了上述注释,请尝试一下,并让我们知道这是否有效。解决方案有效。