Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity default FallbackPolicy interferes with StatusCodePages 404 middleware #32366

Open
lonix1 opened this issue Apr 22, 2024 · 3 comments
Open

Comments

@lonix1
Copy link

lonix1 commented Apr 22, 2024

Description

The linked page's code is incorrect, and its wording could be improved.

When one uses Identity and "secure by default" config - i.e. builder.Services.AddAuthorization(x => x.FallbackPolicy = x.DefaultPolicy); - then attempts to access a non-existent page will go to the login page instead of the error/404 page.

To force requests to go to the error/404 page instead, the linked page's code is given as a solution. But it doesn't work. Perhaps some changes were made in the last few aspnet versions?

This does work:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;

public class SampleAuthorizationMiddlewareResultHandler : IAuthorizationMiddlewareResultHandler
{
    private readonly AuthorizationMiddlewareResultHandler _defaultHandler = new();

    public async Task HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
    {
        if (!authorizeResult.Succeeded &&
            authorizeResult.Challenged &&
            context.GetEndpoint() == null)
        {
            // Return a 404 to make it appear as if the resource doesn't exist.
            context.Response.StatusCode = StatusCodes.Status404NotFound;
            return;
        }

        await _defaultHandler.HandleAsync(next, context, policy, authorizeResult);
    }
}

Also, the "secure by default" docs linked above should be updated to mention this gotcha, else the app doesn't behave as expected. In this scenario, that page is misleading as it states:

The fallback authorization policy requires all users to be authenticated, except for Razor Pages, controllers, or action methods with an authorization attribute. For example, Razor Pages, controllers, or action methods with [AllowAnonymous] or [Authorize(PolicyName="MyPolicy")] use the applied authorization attribute rather than the fallback authorization policy.

It should state that there is one case where the [AllowAnonymous] attribute is ignored, and that should link to the doc discussed above.

Similarly, the status code pages doc should include a warning box with the same note.

Page URL

https://learn.microsoft.com/en-us/aspnet/core/security/authorization/customizingauthorizationmiddlewareresponse?view=aspnetcore-8.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/security/authorization/customizingauthorizationmiddlewareresponse.md

Document ID

d0147dca-5a50-c83b-ae92-8a7c4ea4f69f

Article author

@Rick-Anderson

@lonix1
Copy link
Author

lonix1 commented Apr 22, 2024

Background info for this issue on StackOverflow.

@Rick-Anderson
Copy link
Contributor

@lonix1 I think when using template generated Razor Pages code, unauthorized access redirects to the sign in page.

@Tratcher please assign a reviewer.

@Tratcher
Copy link
Member

@adityamandaleeka and @mkArtakMSFT should be assigning reviewers now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants