|
1 | | -using System.Diagnostics; |
| 1 | +using System.IdentityModel.Tokens.Jwt; |
2 | 2 | using CarvedRock.Data; |
3 | 3 | using CarvedRock.Domain; |
4 | 4 | using Hellang.Middleware.ProblemDetails; |
5 | 5 | using Microsoft.Data.Sqlite; |
6 | 6 | using CarvedRock.Api; |
| 7 | +using Microsoft.Extensions.Options; |
| 8 | +using Microsoft.IdentityModel.Tokens; |
| 9 | +using Swashbuckle.AspNetCore.SwaggerGen; |
7 | 10 |
|
8 | 11 | var builder = WebApplication.CreateBuilder(args); |
9 | 12 | builder.Services.AddProblemDetails(opts => |
|
19 | 22 | opts.Rethrow<SqliteException>(); |
20 | 23 | opts.MapToStatusCode<Exception>(StatusCodes.Status500InternalServerError); |
21 | 24 | }); |
22 | | -//builder.Logging.AddFilter("CarvedRock", LogLevel.Debug); |
23 | 25 |
|
24 | | -// var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
25 | | -// var tracePath = Path.Join(path, $"Log_CarvedRock_{DateTime.Now.ToString("yyyyMMdd-HHmm")}.txt"); |
26 | | -// Trace.Listeners.Add(new TextWriterTraceListener(System.IO.File.CreateText(tracePath))); |
27 | | -// Trace.AutoFlush = true; |
| 26 | +JwtSecurityTokenHandler.DefaultMapInboundClaims = false; |
| 27 | +builder.Services.AddAuthentication("Bearer") |
| 28 | + .AddJwtBearer("Bearer", options => |
| 29 | + { |
| 30 | + options.Authority = "https://demo.duendesoftware.com"; |
| 31 | + options.Audience = "api"; |
| 32 | + options.TokenValidationParameters = new TokenValidationParameters |
| 33 | + { |
| 34 | + NameClaimType = "email" |
| 35 | + }; |
| 36 | + }); |
28 | 37 |
|
29 | | -// Services |
30 | 38 | builder.Services.AddControllers(); |
31 | 39 | // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle |
32 | 40 | builder.Services.AddEndpointsApiExplorer(); |
| 41 | +builder.Services.AddTransient<IConfigureOptions<SwaggerGenOptions>, SwaggerOptions>(); |
33 | 42 | builder.Services.AddSwaggerGen(); |
34 | 43 |
|
35 | 44 | builder.Services.AddScoped<IProductLogic, ProductLogic>(); |
36 | | - |
37 | 45 | builder.Services.AddDbContext<LocalContext>(); |
38 | 46 | builder.Services.AddScoped<ICarvedRockRepository, CarvedRockRepository>(); |
39 | 47 |
|
40 | 48 | var app = builder.Build(); |
41 | 49 |
|
42 | | -app.UseMiddleware<CriticalExceptionMiddleware>(); |
43 | | -app.UseProblemDetails(); |
44 | | - |
45 | 50 | using (var scope = app.Services.CreateScope()) |
46 | 51 | { |
47 | 52 | var services = scope.ServiceProvider; |
48 | 53 | var context = services.GetRequiredService<LocalContext>(); |
49 | 54 | context.MigrateAndCreateData(); |
50 | 55 | } |
51 | 56 |
|
52 | | -// HTTP request pipeline |
| 57 | +app.UseMiddleware<CriticalExceptionMiddleware>(); |
| 58 | +app.UseProblemDetails(); |
| 59 | + |
53 | 60 | if (app.Environment.IsDevelopment()) |
54 | 61 | { |
55 | 62 | app.UseSwagger(); |
56 | | - app.UseSwaggerUI(); |
| 63 | + app.UseSwaggerUI(options => |
| 64 | + { |
| 65 | + options.OAuthClientId("interactive.public.short"); |
| 66 | + options.OAuthAppName("CarvedRock API"); |
| 67 | + options.OAuthUsePkce(); |
| 68 | + }); |
57 | 69 | } |
58 | 70 | app.MapFallback(() => Results.Redirect("/swagger")); |
59 | 71 | app.UseHttpsRedirection(); |
| 72 | +app.UseAuthentication(); |
60 | 73 | app.UseAuthorization(); |
61 | | -app.MapControllers(); |
| 74 | +app.MapControllers().RequireAuthorization(); |
62 | 75 |
|
63 | 76 | app.Run(); |
0 commit comments