Stax.SwaggerAuthorization
0.2.0-alpha
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
<PackageReference Include="Stax.SwaggerAuthorization" Version="0.2.0-alpha" />
paket add Stax.SwaggerAuthorization --version 0.2.0-alpha
#r "nuget: Stax.SwaggerAuthorization, 0.2.0-alpha"
// 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 | Versions 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. |
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
-
net5.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
-
net6.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
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,259 | 10/6/2022 |
1.0.0 | 413 | 10/6/2022 |
0.2.0-alpha | 747 | 7/24/2022 |
0.1.0-alpha | 186 | 7/22/2022 |