简体   繁体   中英

How can I selectively enforce HTTPS on a health check endpoint created using ASP.NET Core Middleware Health Checks?

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.

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" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM