Basic auth for healthchecks in asp.net core

Viewed 1159

I have an ASP.NET Core 3.1 API which already uses OAuth for authorization. I added healthchecks middleware and I would like to make it protected with basic auth so that client which check the health status can simply provide credentials to check the service health. So far I did not manage to configure it for the healthcheck endpoint. Any clue how this can be added?

1 Answers

You can use RequireAuthorization method. For example:

app.UseEndpoints(endpoints =>
{
    endpoints.MapHealthChecks("/health").RequireAuthorization();
});
Related