默认情况下,FallbackPolicy核心3允许设置asp.net以确保端点的安全:
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});这是一个很好的特性,但我也有一个HealthCheck端点,现在需要授权。
services.AddHealthChecks();
[...]
app.UseEndpoints(endpoints => {
endpoints.MapHealthChecks("/health");
endpoints.MapControllers();
});如何允许匿名访问HealthCheck端点(没有身份验证或授权)?
发布于 2020-02-17 12:28:49
我遇到了完全相同的问题,所以我希望这有助于更令人满意的实现:
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute().RequireAuthorization();
endpoints.MapHealthChecks("/health").WithMetadata(new AllowAnonymousAttribute());
});发布于 2021-04-11 05:59:37
从.NET 5开始,有一个更清晰的新的方法-- AllowAnonymous()
app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/health").AllowAnonymous();
});发布于 2019-12-18 09:00:40
您可以在使用HealthCheckMiddleware之前调用AuthenticationMiddleware:
app.Map("/health",appbuilder =>{
appbuilder.UseMiddleware<HealthCheckMiddleware>();
});
// or
// app.UseHealthChecks("/health");
app.UseRouting();
// make sure the authentication middleware runs after the health check middleware
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});https://stackoverflow.com/questions/59387914
复制相似问题