dotnet / dotnet/AspNetCore.Docs

Identity default FallbackPolicy interferes with StatusCodePages 404 middleware

Open
#32,366 4 comments 1 reaction 0 assignees View on GitHub
aspnet-core/svc security/subsvc Source - Docs.ms
Dominant language
C#
Stars
13.1k
Forks
24.6k
Avg merge
1d 3h
Merged PRs (30d)
97

Description

### Description

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

When one uses Identity and ["secure by default"](https://learn.microsoft.com/en-us/aspnet/core/security/authorization/secure-data?view=aspnetcore-7.0#require-authenticated-users) 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:
```cs
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](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling#usestatuscodepages) 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.