I have this web api project in NET 6. In this project, I have a authentication controller and I have an endpoint which authenticate based on username and password.
[Route("api/[controller]")]
[ApiController]
public class AuthenticationController : ControllerBase
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IdbContext _dbContext;
public AuthenticationController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, IdbContext _dbContext)
{
_userManager = userManager;
_signInManager = signInManager;
_dbContext = dbContext;
}
[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> PasswordSignInAsync([FromBody] LoginRequest loginRequest)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(loginRequest.Username, loginRequest.Password, isPersistent: false, lockoutOnFailure: false);
if (result.Succeeded)
{
var armsUser = _dbContext.Users
.FirstOrDefault(u => u.Email == loginRequest.Username);
if (armsUser == null) {
await _signInManager.SignOutAsync();
return BadRequest();
}
ApplicationUser user = await _userManager.FindByEmailAsync(loginRequest.Username);
if (user == null)
{
return NotFound($"Unable to load user with username '{loginRequest.Username}'.");
}
user.LastSignInDate = DateTime.Now;
await _userManager.UpdateAsync(user);
await _signInManager.RefreshSignInAsync(user);
return Ok("Authenticated!");
}
await _signInManager.SignOutAsync();
return BadRequest("Invalid username or password.");
}
return BadRequest(ModelState);
}
}
and this is the protected resources.
[Route("api/[controller]")]
[ApiController]
public class MarketingEventController : ControllerBase
{
private readonly IMediator _mediator;
/// <summary>
///
/// </summary>
/// <param name="mediator"></param>
public MarketingEventController(IMediator mediator)
{
_mediator = mediator;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult> GetLatest()
{
try
{
var query = new GetLatestQuery();
var result = await _mediator.Send(query);
return Ok(result);
}
catch (Exception ex)
{
return Problem(ex.Message);
}
}
}
and some related code in program.cs
...
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers().RequireAuthorization();
If I am authenticated, I get 200 status code as expected. But If I am not authentication and call /api/MarketingEvent, why I get 404 status code instead of 401 status code? Even if I decorate the marketing controller with [Authorize], it still behaves the same.


RequireAuthorizationregisters a convention to apply[Authorize]to all controllers so doing it manually should lead to no difference. Additionally, I would like to inquire regarding framework - you mention .NET 6 and tag is ASP.NET. I presume this relates to ASP.NET Core, not ASP.NET 4.