I’ve configured health checks using the middleware in an MVC application, but have been asked to enforce https on the health endpoint for security reasons. For separate reasons, I can’t enforce it across the service as a whole, and so I was wondering if there was a mechanism to selectively do this to just the health endpoint. While it’s possible to do this with a controller, but I can’t seem to achieve it for the endpoint created by the middleware.
Is there any built-in way to achieve this? Could I create some sort of custom middleware as a wrapper to achieve it?
I’ve looked into EndpointBuilders, conventions, RequireHttps, and more, but have been unable to find anything.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It can be solved by specifying port number for https in Startup#Configure
app.UseEndpoints(endpoints =>
{
// ... other end-points here
endpoints.MapHealthChecks("/readiness").RequireHost("*:5001", "*:443");
});
Tested and verified on my computer with Kestrel that uses port 5000 for http and 5001 for https as defaults. If your production environment does not use port 443, you can remove , "*:443".
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0