I am trying to to Minimal API in an ASP.NET Core app. I added the following endpoint
app.MapPost("api/content", CallbackAsync);
The defined the CallbackAsync as a static function as the following
[Authorize(AuthenticationSchemes = "Api")]
private static async Task<IResult> CallbackAsync(
IAuthorizationService authorizationService,
HttpContext httpContext)
{
// ...
return Results.Ok(...);
}
Authentication is failing. when I use controller, adding [Authorize(AuthenticationSchemes = "Api")] works but not with Minimal API.
How can I apply [Authorize(AuthenticationSchemes = "Api")] with minimal API?
you need to configure authentication middleware explicitly. UseAuthentication() and UseAuthorization() adds required middlewares to the pipeline.