Policy based authorization on razor pages

Curious George

I trying to set up policy-based authorization on razor pages on Core2.1.

I have set up the policy and decorated the razor page with the authorize attribute. I cannot figure what am I doing wrong or if something else needs to be done, but I cannot get the page to authorize. It always gives me

No web page was found for the web address:

localhost/ADENETCore/Account/AccessDenied?ReturnUrl=%2FADENETCore%2FContact

Can you please point me in the right direction?

ConfigureServices:

services.AddAuthorization(options =>
                {
                    options.AddPolicy("AtLeast21", policy =>
                        policy.Requirements.Add(new MinimumAgeRequirement(21)));
                });
            services.AddMvc().AddRazorPagesOptions(options =>
                    {
                        options.Conventions.AuthorizePage("/Contact", "AtLeast21"); // with policy
                    })
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddSessionStateTempDataProvider();

Configure:

    app.UseAuthentication();
    app.UseMvc();

Policy Requirement:

    public class MinimumAgeRequirement : IAuthorizationRequirement
    {
     public int MinimumAge { get; private set; }

     public MinimumAgeRequirement(int minimumAge)
     {
      MinimumAge = minimumAge;
     }
    }

Policy Handler:

public class MinimumAgeHandler : AuthorizationHandler<MinimumAgeRequirement>
        {
            protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
                                                           MinimumAgeRequirement requirement)
            {
    context.Succeed(requirement);
     return Task.CompletedTask;

    }
}

Razor Page:

[Authorize(Policy = "AtLeast21")]
public class ContactModel : PageModel  

It is redirecting to the Account/AccessDenied page

Neville Nazerane

You need to add your authorization handlers as singletons.

services.AddSingleton<IAuthorizationHandler, MinimumAgeHandler>();

For more info check: https://docs.microsoft.com/en-us/aspnet/core/security/authorization/resourcebased?view=aspnetcore-2.2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Role based Authorization for Razor Pages

IP based authorization policy with Attributes

Authorization in ASP .NET Core Razor pages

Configuring Policy-based Authorization in Blazor

ASP.Net MVC Custom Authorization Policy Provider in razor view

Routing based on URL parameter in Razor Pages

Render html conditionally based on route in Razor Pages

return partial view razor pages based on property

Azure media services: Customize ContentKey authorization policy based on backend data

UnauthorizedAccessException behavior in ASP.NET Core policy based authorization

ASP.NET Core Custom Policy Based Authorization - unclear

Policy based Authorization not working in asp.net core

How to access dbcontext & session in Custom Policy-Based Authorization

asp.net core 2.0 - Claims and policy based Authorization

No authenticationScheme was specified, and there was no DefaultForbidScheme found with custom policy based authorization

How to attach needed claims to tokens with policy-based authorization?

Razor Pages and Razor Views

Request-based Localization of Razor pages in Asp.Net Core

Authorization Policy without model

Authorization Policy With Multiple Claims

Custom Policy for Authorization

Catching a failed authorization policy

Dependency Injection on Authorization Policy

Policy-based authorization - Auth0 Authentication - Always Returns Forbidden 403

How to access current HttpContext in ASP.NET Core 2 Custom Policy-Based Authorization with AuthorizationHandlerContext

Adding a Policy-based Authorization skips JWT bearer token authentication check?

Does ASP.NET Core's policy-based authorization require specific claim types in the token?

User.IsInRole always returns false in View or code using Policy based Authorization

Dotnet JWT Bearer TokenValidationParameters issue - Unauthorized? Role(Policy) based authorization doesnt work