66 lines
2.0 KiB
C#
66 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.OpenApi;
|
|
using System.Collections.Generic;
|
|
|
|
namespace MikrocopApi.Extensions;
|
|
|
|
public static class SwaggerExtensions
|
|
{
|
|
public static IServiceCollection AddSwaggerWithJwtAuth(this IServiceCollection services)
|
|
{
|
|
services.AddEndpointsApiExplorer();
|
|
services.AddSwaggerGen(options =>
|
|
{
|
|
const string schemeName = "Bearer";
|
|
options.SwaggerDoc("v1", new OpenApiInfo
|
|
{
|
|
Title = "Mikrocop API",
|
|
Version = "v1"
|
|
});
|
|
|
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Name = "Authorization",
|
|
Type = SecuritySchemeType.Http,
|
|
Scheme = "bearer",
|
|
BearerFormat = "JWT",
|
|
In = ParameterLocation.Header,
|
|
Description = "Enter 'Bearer' followed by a space and your token, e.g., 'Bearer abc123'"
|
|
});
|
|
|
|
|
|
options.AddSecurityRequirement(document => new OpenApiSecurityRequirement
|
|
{
|
|
[new OpenApiSecuritySchemeReference("bearer", document)] = []
|
|
});
|
|
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IApplicationBuilder UseSwaggerWithJwtAuth(this IApplicationBuilder app)
|
|
{
|
|
app.UseSwagger(options =>
|
|
{
|
|
options.PreSerializeFilters.Add((swaggerDoc, _) =>
|
|
{
|
|
swaggerDoc.Security =
|
|
[
|
|
new OpenApiSecurityRequirement
|
|
{
|
|
[new OpenApiSecuritySchemeReference("Bearer", swaggerDoc, null)] = new List<string>()
|
|
}
|
|
];
|
|
});
|
|
});
|
|
app.UseSwaggerUI(options =>
|
|
{
|
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Mikrocop API v1");
|
|
options.EnablePersistAuthorization();
|
|
});
|
|
return app;
|
|
}
|
|
}
|