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

Commit a710c6d

Browse files
committed
Aggiunto Swagger DateTime JWT Bearer
1 parent a815c76 commit a710c6d

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace NET6CustomLibrary.Swagger;
2+
3+
public static class SwaggerDateTimeJWTBearer
4+
{
5+
public static IServiceCollection AddSwaggerGenDateTimeJWTBearerConfig(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.MapType<DateOnly>(() => new OpenApiSchema
21+
{
22+
Type = "string",
23+
Format = "date"
24+
});
25+
26+
options.MapType<TimeOnly>(() => new OpenApiSchema
27+
{
28+
Type = "string",
29+
Format = "time",
30+
Example = new OpenApiString(TimeOnly.FromDateTime(System.DateTime.Now).ToString("HH:mm:ss"))
31+
});
32+
33+
options.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme, new OpenApiSecurityScheme
34+
{
35+
In = ParameterLocation.Header,
36+
Description = "Insert the Bearer Token",
37+
Name = HeaderNames.Authorization,
38+
Type = SecuritySchemeType.ApiKey
39+
});
40+
41+
options.AddSecurityRequirement(new OpenApiSecurityRequirement
42+
{
43+
{
44+
new OpenApiSecurityScheme
45+
{
46+
Reference= new OpenApiReference
47+
{
48+
Type = ReferenceType.SecurityScheme,
49+
Id = JwtBearerDefaults.AuthenticationScheme
50+
}
51+
},
52+
Array.Empty<string>()
53+
}
54+
});
55+
56+
if (extendSchema)
57+
options.UseAllOfToExtendReferenceSchemas();
58+
59+
if (xmlCommentsPath is not (null or ""))
60+
options.IncludeXmlComments(xmlCommentsPath);
61+
});
62+
63+
return services;
64+
}
65+
}

0 commit comments

Comments
 (0)