Skip to content

Instantly share code, notes, and snippets.

@Guifgr
Forked from yanandrey/SwaggerApi.csproj
Last active February 19, 2024 13:58
Show Gist options
  • Save Guifgr/041210bd9f80f6ec7f444eec9b61f6cb to your computer and use it in GitHub Desktop.
Save Guifgr/041210bd9f80f6ec7f444eec9b61f6cb to your computer and use it in GitHub Desktop.
Swagger Configuration
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</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