|
3 | 3 | using Microsoft.AspNetCore.Mvc.Authorization; |
4 | 4 | using Microsoft.Identity.Web; |
5 | 5 | using Microsoft.IdentityModel.JsonWebTokens; |
6 | | -using Microsoft.OpenApi.Models; |
| 6 | +using Microsoft.OpenApi; |
| 7 | +using UserApiTwo; |
7 | 8 |
|
8 | 9 | var builder = WebApplication.CreateBuilder(args); |
9 | 10 |
|
|
13 | 14 | builder.Services.AddMicrosoftIdentityWebApiAuthentication( |
14 | 15 | builder.Configuration, "AzureAd"); |
15 | 16 |
|
16 | | -builder.Services.AddSwaggerGen(c => |
| 17 | +builder.Services.AddOpenApi(options => |
17 | 18 | { |
18 | | - // add JWT Authentication |
19 | | - var securityScheme = new OpenApiSecurityScheme |
20 | | - { |
21 | | - Name = "JWT Authentication", |
22 | | - Description = "Enter JWT Bearer token **_only_**", |
23 | | - In = ParameterLocation.Header, |
24 | | - Type = SecuritySchemeType.Http, |
25 | | - Scheme = "bearer", // must be lower case |
26 | | - BearerFormat = "JWT", |
27 | | - Reference = new OpenApiReference |
28 | | - { |
29 | | - Id = JwtBearerDefaults.AuthenticationScheme, |
30 | | - Type = ReferenceType.SecurityScheme |
31 | | - } |
32 | | - }; |
33 | | - c.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme); |
34 | | - c.AddSecurityRequirement(new OpenApiSecurityRequirement |
35 | | - { |
36 | | - {securityScheme, Array.Empty<string>()} |
37 | | - }); |
38 | | - |
39 | | - c.SwaggerDoc("v1", new OpenApiInfo |
40 | | - { |
41 | | - Title = "User API Two", |
42 | | - Version = "v1", |
43 | | - Description = "User API Two", |
44 | | - Contact = new OpenApiContact |
45 | | - { |
46 | | - Name = "damienbod", |
47 | | - Email = string.Empty, |
48 | | - Url = new Uri("https://damienbod.com/"), |
49 | | - }, |
50 | | - }); |
| 19 | + //options.UseTransformer((document, context, cancellationToken) => |
| 20 | + //{ |
| 21 | + // document.Info = new() |
| 22 | + // { |
| 23 | + // Title = "My API", |
| 24 | + // Version = "v1", |
| 25 | + // Description = "API for Damien" |
| 26 | + // }; |
| 27 | + // return Task.CompletedTask; |
| 28 | + //}); |
| 29 | + options.AddDocumentTransformer<BearerSecuritySchemeTransformer>(); |
51 | 30 | }); |
52 | 31 |
|
53 | 32 | builder.Services.AddControllers(options => |
|
69 | 48 | app.UseHsts(); |
70 | 49 | } |
71 | 50 |
|
72 | | -app.UseSwagger(); |
73 | | -app.UseSwaggerUI(c => |
74 | | -{ |
75 | | - c.SwaggerEndpoint("/swagger/v1/swagger.json", "User API Two"); |
76 | | - c.RoutePrefix = string.Empty; |
77 | | -}); |
78 | | - |
79 | 51 | app.UseHttpsRedirection(); |
80 | 52 |
|
81 | 53 | app.UseRouting(); |
|
85 | 57 |
|
86 | 58 | app.MapControllers(); |
87 | 59 |
|
| 60 | +app.MapOpenApi("/openapi/v1/openapi.json"); |
| 61 | + |
| 62 | +if (app.Environment.IsDevelopment()) |
| 63 | +{ |
| 64 | + app.UseSwaggerUI(options => |
| 65 | + { |
| 66 | + options.SwaggerEndpoint("/openapi/v1/openapi.json", "v1"); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
88 | 70 | app.Run(); |
0 commit comments