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?
2
Answers
you need to configure authentication middleware explicitly.
UseAuthentication() and UseAuthorization() adds required middlewares to the pipeline.
Similar to a standard controller attribute. In Program.cs file,
For additional information this link should be helpful.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/security?view=aspnetcore-8.0
Ensure that any other dependencies that rely on DI are also injected using
The DI part should remain the same.