Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use authorization by default without [Authorize] in ASP.NET Core 5

I have an ASP.NET Core 5 application.

I've got authorization working with the [Authorize] attribute above the controller endpoint.

How can I make it use authorization without having to use the [Authorize] attribute? Here is how I set up my authorization:

I have this in ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{...
        FirebaseApp.Create(new AppOptions()
        {
            Credential = GoogleCredential.FromFile("firebase_admin_sdk.json"),
        });

        services
        .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.Authority = "https://securetoken.google.com/vepo-xxx";
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidIssuer = "https://securetoken.google.com/vepo-xxx",
                ValidateAudience = true,
                ValidAudience = "vepo-xxx",
                ValidateLifetime = true
            };
        });
...}

And in I Configure have:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, VepoContext context, ISearchIndexService searchIndexService)
{...
    app.UseAuthentication();
    app.UseAuthorization();
...}
like image 382
BeniaminoBaggins Avatar asked Oct 30 '25 14:10

BeniaminoBaggins


1 Answers

See the docs for Require authenticated users. You can add a RequireAuthenticatedUser() to all controllers:

public void ConfigureServices(IServiceCollection services)
{

    ...

    services.AddControllers(config =>
    {
        // using Microsoft.AspNetCore.Mvc.Authorization;
        // using Microsoft.AspNetCore.Authorization;
        var policy = new AuthorizationPolicyBuilder()
                         .RequireAuthenticatedUser()
                         .Build();
        config.Filters.Add(new AuthorizeFilter(policy));
    });
like image 117
SpruceMoose Avatar answered Nov 02 '25 11:11

SpruceMoose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!