How atakancelik/lockedswagger works
Project type and purpose
• This is a small ASP.NET Core “middleware library” project (distributed as a sample solution) whose sole purpose is to inject Basic Authentication protection into the Swagger UI of any ASP.NET Core Web API.
• It is not a full-stack application with separate frontend/backends; it is a single-project extension that plugs into the ASP.NET Core pipeline.
File structure
• Top-level solution file: LockedSwagger.sln
• Single project directory: LockedSwagger/ containing:
• Controllers (default WeatherForecastController)
• Program.cs (ASP.NET Core host builder)
• SwaggerBasicAuthMiddleware.cs (the core middleware component)
• appsettings.json (+ Development variant) for binding Authentication:Users
• Standard .NET config (Properties/launchSettings.json) and project file.
• No separate “frontend” or “database” folders. No external microservices.
README for additional insights
• Describes how to wire up the middleware in Startup.Configure (or in Program.cs for .NET 6+ minimal hosting).
• Explains runtime behavior: on requests to /swagger, the middleware issues a 401 challenge if no or invalid Basic credentials are provided; otherwise it falls through to the Swagger generator.
Main components and how to represent them
a. ASP.NET Core Host
• Kestrel Web Server
• Middleware Pipeline
b. SwaggerBasicAuthMiddleware
• Reads configured users from IOptions bound to appsettings.json
• Intercepts requests whose path matches /swagger (Swagger endpoints)
• Validates Authorization: Basic header
c. Swagger Generator (Swashbuckle)
• Produces the Swagger JSON & UI
d. Configuration Store
• In-memory binding to appsettings.json → list of valid credentials
e. Sample Controller (WeatherForecastController)
• Represents the downstream API controllers registered in the pipeline
f. Client (Browser)
• Initiates HTTP request to /swagger endpoint
Relationships and interactions
• Client sends HTTP GET /swagger → Enters Kestrel →
1. Hits ASP.NET Core middleware pipeline
2. Reaches SwaggerBasicAuthMiddleware
• Reads Authorization header
• Checks credentials against Configuration Store
• If invalid: returns 401 Unauthorized (end of pipeline)
• If valid: calls next() →
3. Falls through to Swashbuckle’s Swagger generator → returns UI/JSON
• Downstream API controllers (e.g., WeatherForecastController) are unaffected by this middleware except at the Swagger UI boundary.
Architectural patterns and design principles
• Middleware pattern (chain of responsibility) to intercept and optionally short-circuit HTTP requests
• Configuration-as-Code (appsettings.json → IOptions) for credentials
• Separation of concerns: authentication logic lives in a reusable middleware class, not tangled with controllers
• Minimal external dependencies (pure ASP.NET Core + Swashbuckle + .NET configuration system)