Authorization in ASP .NET Core Razor pages

shertu

I am unable to implement policy-based authorization in ASP .NET Core for an action on a razor page.

I read through this comprehensive document on authorization and used its examples as guidence.

Razor page action code:

[Authorize(Policy = "test")]
public async Task<IActionResult> OnGetCreateAsync(string id)

Code in service configuration:

_ = services.AddAuthorization(options => {
    options.AddPolicy("test", policy =>
        policy.RequireAssertion(context =>
            false));
});

I expect that if I call the action or endpoint service, e.g.

GET /Account?handler=Create

then the request will be denied with a 403 status response because the "test" policy states that everyone is unauthorized. However, in actual practice, the action is successfully called.

Kirk Larkin

Razor Pages doesn't currently support [Authorize] at the handler level. i.e. You can only authorise a page as a whole, on the PageModel itself, as noted in the docs:

Policies can not be applied at the Razor Page handler level, they must be applied to the Page.

If authorising the page as a whole isn't a workable solution, you might need to move your OnGetCreateAsync handler into a controller/action pair, which can be attributed with [Authorize] accordingly.

There's also a related GitHub issue in the docs for this:

The [Authorize] filter attribute has been supported since 2.0 in Razor Pages, but note that it works at the page model class level

If you need a better workaround, see akbar's answer and Jim Yabro's answer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Authorization in ASP.NET Core razor

Asp.Net Core Razor Pages authorization handlers for policies with Authorize attribute on PageModel still run after executing policies set to folders

Rename Pages/Shared directory in ASP.NET Core Razor Pages

ASP.NET Core Razor pages vs Full MVC Core

How to redirect on ASP.Net Core Razor Pages

Bootstrap Alerts in Asp.net Core Razor Pages

ASP.NET Core Razor Pages button is not working

Implement Sorting With ASP.NET Core Razor Pages

ASP net Core Razor Pages - split models for EF and views

Specify language for ASP.NET Core 2 Localizer in Razor Pages

ASP.NET Core Razor Pages Routing With parameters

Using a DI with partial views in asp.net core razor pages

Asp .Net Core 2.2 Razor Pages Ajax Call Post not working

Razor Pages Asp.Net-Core install FullCalendar

How to deny anonymous users in asp.net core razor pages?

Route Override not working in asp.net core 3.1 razor pages

ASP .Net Core with Razor Pages Vs Angular for UI

Asp.Net Core Razor Pages Exception Handling breaks the application

RedirectToPage() not working in ASP.NET Core 3 Razor Pages

ASP.NET Core Razor Pages model binding

NotSupportedException in Asp.NET Core MVC Razor Pages Rendering

How to extend an ASP.NET Core MVC project by Razor Pages?

How to implement pagination in asp.net core razor pages

ASP.NET Core Razor pages - not binding on POST Request

common services in asp.net core with razor pages

ViewComponent not found in Asp.NET Core 2.2 using Razor Pages

Using razor pages in ASP.NET Core minimal API

Creating An Empty ASP.NET Core 2.0 Razor Pages Project

Progressive web app with asp.net core 2.0 Razor Pages