Skip to content

Instantly share code, notes, and snippets.

@yanandrey
Last active February 24, 2022 22:19
Show Gist options
  • Save yanandrey/44336d8a22d4a5744d947d8c4a508751 to your computer and use it in GitHub Desktop.
Save yanandrey/44336d8a22d4a5744d947d8c4a508751 to your computer and use it in GitHub Desktop.
Swagger Configuration
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>
</Project>
public static class SwaggerServiceCollection
{
public static IServiceCollection AddSwaggerServices(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Api",
Version = "v1"
});
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme {
In = ParameterLocation.Header,
Description = "'Bearer' + JWT Token",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
return services;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment