Closed
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Having a method as such, the User property of the controller is never assigned the user from the bearer token from the header when AllowAnonymous
is added. Docs states that if you add AllowAnonymous
along with Authorize
, it will allow anonymous when no authentication is present, and authenticate and set User property of controller when authentication is present
[AllowAnonymous]
[Authorize]
[HttpGet("test1", Name = "Test1")]
public async Task<ActionResult<TestResponse>> Test()
{
this.User <-- always no claims
...
}
Expected Behavior
If auth header is present it should verify it and set User property of controller.
Steps To Reproduce
StartUp
...
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
...
var app = builder.Build();
if (app.Environment.IsProduction())
app.UsePathBase("/api/v2");
if (app.Environment.IsDevelopment())
{
app.UseServerTiming();
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost
});
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Controller
[Route("account")]
[ApiController]
[Authorize]
[Produces("application/json")]
public class AccountController : BaseController
{
[AllowAnonymous]
[Authorize] <-- tried adding it here as well in case it needed it here
[HttpGet("test1", Name = "Test1")]
public async Task<ActionResult<TestResponse>> Test()
{
this.User <-- always no claims
...
}
}
Exceptions (if any)
No response
.NET Version
6.0
Anything else?
No response