SwaggerAuth.Core 1.0.1

dotnet add package SwaggerAuth.Core --version 1.0.1
                    
NuGet\Install-Package SwaggerAuth.Core -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SwaggerAuth.Core" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SwaggerAuth.Core" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="SwaggerAuth.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SwaggerAuth.Core --version 1.0.1
                    
#r "nuget: SwaggerAuth.Core, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SwaggerAuth.Core@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SwaggerAuth.Core&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=SwaggerAuth.Core&version=1.0.1
                    
Install as a Cake Tool

SwaggerAuth.Core

A flexible and secure Swagger authentication middleware for ASP.NET Core Web APIs. Supports multiple document authentication with configurable credentials.

Author: Kenan İLGÜN
GitHub: github.com/kenanilgun/swagger-auth-core

Features

  • 🔐 Basic Authentication for Swagger documentation
  • 📚 Multiple Document Support - Secure different API groups separately
  • ⚙️ Flexible Configuration - Configure authentication per document
  • 🚀 Easy Integration - Simple extension methods for quick setup
  • 🔒 Security First - Only configured documents require authentication
  • 📖 Well Documented - Comprehensive XML documentation

Installation

dotnet add package SwaggerAuth.Core

Quick Start

1. Configure Authentication Credentials

Add your Swagger authentication configuration to appsettings.json:

{
  "Swagger": {
    "Auth": {
      "v1-admin": {
        "Username": "admin",
        "Password": "K9#mP2$vL8nQ"
      },
      "v1-public": {
        "Username": "public",
        "Password": "X7@jR5&hF3wE"
      }
    }
  }
}

2. Register Services

In your Program.cs or Startup.cs:

using SwaggerAuth.Core;

// Add Swagger authentication services
builder.Services.AddSwaggerAuth();

// Or with custom options
builder.Services.AddSwaggerAuth(options =>
{
    options.ConfigurationSection = "Swagger:Auth";
    options.RequireAuthForAllDocuments = false;
    options.CustomErrorMessage = "Access denied to API documentation";
});

3. Add Middleware

// Add the middleware to your pipeline
app.UseSwaggerAuth();

4. Configure Swagger Documents

builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1-admin", new OpenApiInfo { Title = "Admin API", Version = "v1" });
    c.SwaggerDoc("v1-public", new OpenApiInfo { Title = "Public API", Version = "v1" });
    c.SwaggerDoc("v1-internal", new OpenApiInfo { Title = "Internal API", Version = "v1" });
});

Configuration Options

SwaggerAuthOptions

Property Type Default Description
ConfigurationSection string "Swagger:Auth" Configuration section where credentials are stored
RequireAuthForAllDocuments bool false Whether to require auth for all documents by default
CustomErrorMessage string? null Custom error message for authentication failures

Configuration Structure

{
  "Swagger": {
    "Auth": {
      "document-name": {
        "Username": "username",
        "Password": "password"
      }
    }
  }
}

Usage Examples

Basic Setup

// Program.cs
var builder = WebApplication.CreateBuilder(args);

// Add services
builder.Services.AddSwaggerAuth();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1-admin", new OpenApiInfo { Title = "Admin API", Version = "v1" });
    c.SwaggerDoc("v1-public", new OpenApiInfo { Title = "Public API", Version = "v1" });
});

var app = builder.Build();

// Add middleware
app.UseSwaggerAuth();
app.UseSwagger();
app.UseSwaggerUI();

app.Run();

Advanced Configuration

builder.Services.AddSwaggerAuth(options =>
{
    options.ConfigurationSection = "MyApi:Documentation:Auth";
    options.RequireAuthForAllDocuments = true;
    options.CustomErrorMessage = "Please contact administrator for access";
});

Custom Configuration Section

{
  "MyApi": {
    "Documentation": {
      "Auth": {
        "v1-secure": {
          "Username": "secureuser",
          "Password": "securepass123"
        }
      }
    }
  }
}

Security Best Practices

  1. Use Strong Passwords: Use complex passwords with special characters
  2. Environment-Specific Configuration: Use different credentials for different environments
  3. Regular Password Rotation: Change passwords periodically
  4. HTTPS Only: Always use HTTPS in production
  5. Minimal Access: Only secure documents that need protection

Examples

Multiple API Groups

{
  "Swagger": {
    "Auth": {
      "v1-admin": {
        "Username": "admin",
        "Password": "AdminPass123!"
      },
      "v1-partner": {
        "Username": "partner",
        "Password": "PartnerPass456!"
      },
      "v1-internal": {
        "Username": "internal",
        "Password": "InternalPass789!"
      }
    }
  }
}

Public and Private APIs

builder.Services.AddSwaggerGen(c =>
{
    // Public API - no authentication required
    c.SwaggerDoc("v1-public", new OpenApiInfo { Title = "Public API", Version = "v1" });
    
    // Private API - authentication required
    c.SwaggerDoc("v1-private", new OpenApiInfo { Title = "Private API", Version = "v1" });
});

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For support and questions, please open an issue on GitHub or contact Kenan İLGÜN.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 618 6/20/2025