I’ve got Authorize
attributes on my razor pages:
[Authorize(Roles = "Admin")]
And I’ve tried both of the below in Program.cs
:
app.UseStatusCodePages(async context =>
{
var response = context.HttpContext.Response;
if (response.StatusCode == (int)HttpStatusCode.Forbidden)
{
response.Redirect("/Error/Unauthorized");
}
});
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == (int)HttpStatusCode.Forbidden)
{
context.Response.Redirect("/Error/Unauthorized");
}
});
I placed a breakpoint on the status code checks and I noticed they are hit when loading the home page, but when I navigate to any other page they aren’t hit, no check is performed and while a 403 is returned, no redirection occurs and the user gets the browser’s default message about receiving a 403.
What am I missing to make the redirection occur?