Elias Johannes
Elias Johannes

Reputation: 734

Authorize Tag Helper in Razor Pages for page handler method

i have a Razor Pages application where i use Claim Based Authorization. Inside my code i'm using the [Authorize] tag to identify which methods a user is able to access. It works perfectly fine for my class but it doesn't work for my individual page handlers.

After some research it seems that it isn't supported for page handlers. Does anyone know a workaround for this?

My code looks something like this:

[Authorize(Policy = "ListItems")]
public class LicensesModel : PageModel
{ 
    // Only user with the "ListItems" Claim can open this page
    public void OnGet()
    {
         ...
    }

    [Authorize(Policy = "DownloadItem")]
    public IActionResult OnPostDownload(string id)
    {
         // This method can always be accessed, but shouldn't
    }
}

Thanks in advance!

Upvotes: 3

Views: 736

Answers (1)

Mike Brind
Mike Brind

Reputation: 30075

This looks like a candidate for a custom global filter: https://www.learnrazorpages.com/razor-pages/filters.

You would create one that executes when the handler has been selected, and then use context.HandlerMethod.MethodInfo.Name to get the name of the selected handler method. Then you can do your authorisation check based on that.

Upvotes: 1

Related Questions