|
1 | | -using Microsoft.AspNetCore.Authentication.JwtBearer; |
2 | 1 | using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
3 | 2 | using Microsoft.AspNetCore.Authorization; |
4 | 3 | using Microsoft.AspNetCore.Mvc.Authorization; |
5 | 4 | using Microsoft.Identity.Web; |
6 | 5 | using Microsoft.IdentityModel.JsonWebTokens; |
7 | | -using Microsoft.OpenApi.Models; |
8 | 6 |
|
9 | 7 | var builder = WebApplication.CreateBuilder(args); |
10 | 8 |
|
|
23 | 21 | options.TokenValidationParameters.NameClaimType = "name"; |
24 | 22 | }); |
25 | 23 |
|
26 | | -builder.Services.AddSwaggerGen(c => |
| 24 | +builder.Services.AddOpenApi(options => |
27 | 25 | { |
28 | | - // add JWT Authentication |
29 | | - var securityScheme = new OpenApiSecurityScheme |
30 | | - { |
31 | | - Name = "JWT Authentication", |
32 | | - Description = "Enter JWT Bearer token **_only_**", |
33 | | - In = ParameterLocation.Header, |
34 | | - Type = SecuritySchemeType.Http, |
35 | | - Scheme = "bearer", // must be lower case |
36 | | - BearerFormat = "JWT", |
37 | | - Reference = new OpenApiReference |
38 | | - { |
39 | | - Id = JwtBearerDefaults.AuthenticationScheme, |
40 | | - Type = ReferenceType.SecurityScheme |
41 | | - } |
42 | | - }; |
43 | | - c.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme); |
44 | | - c.AddSecurityRequirement(new OpenApiSecurityRequirement |
45 | | - { |
46 | | - {securityScheme, Array.Empty<string>()} |
47 | | - }); |
48 | | - |
49 | | - c.SwaggerDoc("v1", new OpenApiInfo |
50 | | - { |
51 | | - Title = "Web API with roles", |
52 | | - Version = "v1", |
53 | | - Description = "Web API with roles", |
54 | | - Contact = new OpenApiContact |
55 | | - { |
56 | | - Name = "damienbod", |
57 | | - Email = string.Empty, |
58 | | - Url = new Uri("https://damienbod.com/"), |
59 | | - }, |
60 | | - }); |
| 26 | + //options.UseTransformer((document, context, cancellationToken) => |
| 27 | + //{ |
| 28 | + // document.Info = new() |
| 29 | + // { |
| 30 | + // Title = "My API", |
| 31 | + // Version = "v1", |
| 32 | + // Description = "API for Damien" |
| 33 | + // }; |
| 34 | + // return Task.CompletedTask; |
| 35 | + //}); |
| 36 | + options.AddDocumentTransformer<BearerSecuritySchemeTransformer>(); |
61 | 37 | }); |
62 | 38 |
|
63 | 39 | builder.Services.AddAuthorization(policies => |
|
113 | 89 | app.UseDeveloperExceptionPage(); |
114 | 90 | } |
115 | 91 |
|
116 | | -app.UseSwagger(); |
117 | | -app.UseSwaggerUI(c => |
118 | | -{ |
119 | | - c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API with roles"); |
120 | | - c.RoutePrefix = string.Empty; |
121 | | -}); |
122 | | - |
123 | 92 | app.UseHttpsRedirection(); |
124 | 93 |
|
125 | 94 | app.UseRouting(); |
|
129 | 98 |
|
130 | 99 | app.MapControllers(); |
131 | 100 |
|
| 101 | +app.MapOpenApi("/openapi/v1/openapi.json"); |
| 102 | + |
| 103 | +if (app.Environment.IsDevelopment()) |
| 104 | +{ |
| 105 | + app.UseSwaggerUI(options => |
| 106 | + { |
| 107 | + options.SwaggerEndpoint("/openapi/v1/openapi.json", "v1"); |
| 108 | + }); |
| 109 | +} |
| 110 | + |
132 | 111 | app.Run(); |
0 commit comments