SideAuth is a library for ASP.NET Core designed to help developers protect specific Minimal API endpoints with HTTP Basic Authentication or API Keys using minimal configuration.
While operational endpoints—such as OpenAPI/Swagger/Scalar specifications, Prometheus metrics, or health checks—are typically restricted or excluded from production environments, certain deployment scenarios require them to remain accessible but protected. This library allows developers to secure these specific routes with a few lines of code directly during endpoint mapping.
dotnet add package IlyaKo.AspNet.SideAuthservices.AddBasicSideAuth("admin", "SecurePassword123");
services.AddApiKeySideAuth("prometheus-token");app.MapOpenApi().RequireBasicSideAuth();
app.MapScalarApiReference().RequireBasicSideAuth();
app.MapGet("/todos", async (ITodoService service) => await service.GetAll());
app.MapGet("/todos/{id:int}", async (int id, ITodoService service) => await service.GetByIdAsync(id));
app.MapGet("/metrics", () => "Metrics data").RequireApiKeySideAuth();
app.MapGet("/healthz", () => "Healthy").RequireAnySideAuth();Based on the configuration above:
- OpenAPI specifications and Scalar UI require HTTP Basic Authentication (
admin/SecurePassword123) /metricsrequiresprometheus-tokenpassed in the defaultX-API-Keyheader/healthzaccepts either HTTP Basic Authentication or the API Key/todoendpoints remain public