0

I created a custom authorize attribute [AuthorizeAltas] with Blazor Net 5 but is never reached. With a controller works fine but in a Blazor page is not called. This is the razor page:

@page "/Altas"
@attribute [AuthorizeAltas]

and the class implementing the custom attribute:

public class AuthorizeAltasAttribute : AuthorizeAttribute, IAsyncAuthorizationFilter
{
    public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
    {
        Glob.Counter++;  // never executed
        var user = context.HttpContext.User;
        bool isAuthorized = false;
        var usuario = user.Identity.Name.Split("\\")[1].ToLower();
        if (usuario == "josesito") isAuthorized = true;
        if (!isAuthorized)
        {
            context.Result = new UnauthorizedResult();
            return;
        }
    }
}

This is the StartUp:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddSingleton<WeatherForecastService>();
            services.AddControllers();
            services.AddAuthentication(IISDefaults.AuthenticationScheme);
        }

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

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

with a controller action works fine but not in razor pages. The app is a Blazor server side with controllers.

Thanks.

0 Answers0