How to do Authorization for UseHealthCheck middleware

Ashutosh Pareek 26 Reputation points
2021-08-26T07:24:42.247+00:00

For health check I am using UseHealthChecks middleware extension where I am setting path argument to /hc.
How can add or remove authorization here based on config?

I want to achieve similar to below code with UseHealthChecks where I can add or remove RequireAuthorization based on condition.

   app.UseEndpoints(endpoints =>  
   {  
       endpoints.MapHealthChecks("/health").RequireAuthorization();  
   });  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,235 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2021-08-26T14:28:47.883+00:00

    Use an if statement

    app.UseEndpoints(endpoints =>
    {
        var ep = endpoints.MapHealthChecks("/health");
         if (UseHealtChecks) 
              ep.RequireAuthorization();
    });
    

0 additional answers

Sort by: Most helpful