AllowAnonymous permits unauthenticated users to execute a specific action, Razor Page, or API endpoint. Use it when authorization is applied globally or to a broader controller, route group, or endpoint. section 24.2.1 section 25.7
A common need is a public login action inside an otherwise protected controller. Without the exception, an unauthenticated request can be redirected back to the login page indefinitely. Login, error, and password-reset actions commonly need this attribute. section 24.2.1
Leave the login action public
Apply authorization to the controller, then mark only the login action with [AllowAnonymous]. Other actions remain protected, while unauthenticated users can reach the login endpoint.
Choose the form that matches the endpoint style:
- Use [AllowAnonymous] on an MVC action, Razor Page, or minimal API handler. section 24.2.1 section 25.6
- Treat the exception as a deliberate security boundary: it removes authentication requirements, so callers do not need a bearer token. section 25.7
