Stax.SwaggerAuthorization 0.2.0-alpha

This is a prerelease version of Stax.SwaggerAuthorization.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Stax.SwaggerAuthorization --version 0.2.0-alpha                
NuGet\Install-Package Stax.SwaggerAuthorization -Version 0.2.0-alpha                
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="Stax.SwaggerAuthorization" Version="0.2.0-alpha" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Stax.SwaggerAuthorization --version 0.2.0-alpha                
#r "nuget: Stax.SwaggerAuthorization, 0.2.0-alpha"                
#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.
// Install Stax.SwaggerAuthorization as a Cake Addin
#addin nuget:?package=Stax.SwaggerAuthorization&version=0.2.0-alpha&prerelease

// Install Stax.SwaggerAuthorization as a Cake Tool
#tool nuget:?package=Stax.SwaggerAuthorization&version=0.2.0-alpha&prerelease                

Swagger Authorization

Summary

This NuGet library allows you to easily secure your swagger endpoints with a basic username and password. It reads a username and password from your appsettings.json file, which you can then use to access your swagger endpoints.

This helps you expose them on production, without having to setup firewall rules so people can't access them for example.

How to use

This relies on you adding a username and password in your appsettings.json which is picked up by this NuGet library, which then secures anything which starts with /swagger as an endpoint.

Step 1: Add something like this to your appsettings.json, or setup environment variables which your application then loads into your appsettings.json

appsettings.json:
"SwaggerAuthorization": {
    "Username": "uid",
    "Password": "pwd"
}

You can also disable having this login screen occur by adding: "Disabled: true" - which can be useful if you want to add this to your appsettings.Development.json file for example. Eg:

appsettings.Development.json:
"SwaggerAuthorization": {
    "Username": "uid",
    "Password": "pwd",
    "Disabled": true
}
Program.cs/startup

On default projects, your app.UseSwagger(); is in a if (env.IsDevelopment()) statement. You can move this out of this if statement.

Once you have done this, you want to put app.UseSwaggerBasicAuthorization(); above app.UseSwagger();. Your Configure method may now look something like this.

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}

app.UseSwaggerBasicAuthorization(); <--- newly added
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
    
app.UseRouting();

app.UseCors();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

If you run your API project, you should now see a popup screen when you try to access your swagger endpoint.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.1 is compatible. 
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 1,418 10/6/2022
1.0.0 419 10/6/2022
0.2.0-alpha 749 7/24/2022
0.1.0-alpha 188 7/22/2022