Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit b821f5d

Browse files
committed
Aggiunto Swagger JWT Bearer
1 parent 55921dd commit b821f5d

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/NET6CustomLibrary/GlobalUsings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
global using System.Threading.Tasks;
1212
global using MailKit.Net.Smtp;
1313
global using MailKit.Security;
14+
global using Microsoft.AspNetCore.Authentication.JwtBearer;
1415
global using Microsoft.AspNetCore.Builder;
1516
global using Microsoft.AspNetCore.Diagnostics.HealthChecks;
1617
global using Microsoft.AspNetCore.Http;

src/NET6CustomLibrary/NET6CustomLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
2020
<PackageReference Include="MailKit" Version="3.6.0" />
2121
<PackageReference Include="MediatR" Version="12.0.1" />
22+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.15" />
2223
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.15" />
2324
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
2425
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace NET6CustomLibrary.Swagger;
2+
3+
public static class SwaggerJWTBearer
4+
{
5+
public static IServiceCollection AddSwaggerGenJWTBearerConfig(this IServiceCollection services, string title,
6+
string version, string description = "", bool extendSchema = false, string xmlCommentsPath = "")
7+
{
8+
services
9+
.AddEndpointsApiExplorer()
10+
.AddSwaggerGen(options =>
11+
{
12+
options.OperationFilter<CultureAwareOperationFilter>();
13+
options.SwaggerDoc($"{version}", new OpenApiInfo
14+
{
15+
Title = $"{title}",
16+
Version = $"{version}",
17+
Description = $"{description}",
18+
});
19+
20+
options.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme, new OpenApiSecurityScheme
21+
{
22+
In = ParameterLocation.Header,
23+
Description = "Insert the Bearer Token",
24+
Name = HeaderNames.Authorization,
25+
Type = SecuritySchemeType.ApiKey
26+
});
27+
28+
options.AddSecurityRequirement(new OpenApiSecurityRequirement
29+
{
30+
{
31+
new OpenApiSecurityScheme
32+
{
33+
Reference= new OpenApiReference
34+
{
35+
Type = ReferenceType.SecurityScheme,
36+
Id = JwtBearerDefaults.AuthenticationScheme
37+
}
38+
},
39+
Array.Empty<string>()
40+
}
41+
});
42+
43+
if (extendSchema)
44+
options.UseAllOfToExtendReferenceSchemas();
45+
46+
if (xmlCommentsPath is not (null or ""))
47+
options.IncludeXmlComments(xmlCommentsPath);
48+
});
49+
50+
return services;
51+
}
52+
}

0 commit comments

Comments
 (0)