ASP.NET Core Web API Interview Question #61

What does [AllowAnonymous] do?

Authentication, Authorization, JWT & API Security Mid-Level Intermediate

Quick Interview Answer

AllowAnonymous permits an endpoint to be accessed without satisfying Authorize requirements that would otherwise apply to it. It is useful for intentionally public endpoints such as login, registration, or selected public content.

Detailed Explanation

AllowAnonymous permits an endpoint to be accessed without satisfying Authorize requirements that would otherwise apply to it. It is useful for intentionally public endpoints such as login, registration, or selected public content.

Use it deliberately because it overrides authorization requirements for that endpoint.

Code Example

[Authorize]
public class AccountController : ControllerBase
{
    [AllowAnonymous]
    [HttpPost("login")]
    public IActionResult Login(LoginRequest request)
        => Ok();
}