|
1 |
| -CreateWebHostBuilder(args).Build().Run(); |
| 1 | +var builder = WebApplication.CreateBuilder(args); |
2 | 2 |
|
| 3 | +builder.Services.AddSession(opt => |
| 4 | + { |
| 5 | + opt.Cookie.Name = ".eShopWebhooks.Session"; |
| 6 | + }) |
| 7 | + .Configure<WebhookClientOptions>(builder.Configuration) |
| 8 | + .AddHttpClientServices(builder.Configuration) |
| 9 | + .AddCustomAuthentication(builder.Configuration) |
| 10 | + .AddTransient<IWebhooksClient, WebhooksClient>() |
| 11 | + .AddSingleton<IHooksRepository, InMemoryHooksRepository>() |
| 12 | + .AddMvc(); |
| 13 | +builder.Services.AddControllers(); |
| 14 | +var app = builder.Build(); |
3 | 15 |
|
4 |
| -IWebHostBuilder CreateWebHostBuilder(string[] args) => |
5 |
| - WebHost.CreateDefaultBuilder(args) |
6 |
| - .UseStartup<Startup>(); |
| 16 | +var pathBase = app.Configuration["PATH_BASE"]; |
| 17 | +if (!string.IsNullOrEmpty(pathBase)) |
| 18 | +{ |
| 19 | + app.UsePathBase(pathBase); |
| 20 | +} |
| 21 | + |
| 22 | +if (!app.Environment.IsDevelopment()) |
| 23 | +{ |
| 24 | + app.UseExceptionHandler("/Error"); |
| 25 | + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. |
| 26 | +} |
| 27 | + |
| 28 | +app.Map("/check", capp => |
| 29 | +{ |
| 30 | + capp.Run(async (context) => |
| 31 | + { |
| 32 | + if ("OPTIONS".Equals(context.Request.Method, StringComparison.InvariantCultureIgnoreCase)) |
| 33 | + { |
| 34 | + var validateToken = bool.TrueString.Equals(builder.Configuration["ValidateToken"], StringComparison.InvariantCultureIgnoreCase); |
| 35 | + var header = context.Request.Headers[HeaderNames.WebHookCheckHeader]; |
| 36 | + var value = header.FirstOrDefault(); |
| 37 | + var tokenToValidate = builder.Configuration["Token"]; |
| 38 | + if (!validateToken || value == tokenToValidate) |
| 39 | + { |
| 40 | + if (!string.IsNullOrWhiteSpace(tokenToValidate)) |
| 41 | + { |
| 42 | + context.Response.Headers.Add(HeaderNames.WebHookCheckHeader, tokenToValidate); |
| 43 | + } |
| 44 | + context.Response.StatusCode = (int)HttpStatusCode.OK; |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + await context.Response.WriteAsync("Invalid token"); |
| 49 | + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; |
| 50 | + } |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; |
| 55 | + } |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +// Fix samesite issue when running eShop from docker-compose locally as by default http protocol is being used |
| 60 | +// Refer to https://github.com/dotnet-architecture/eShopOnContainers/issues/1391 |
| 61 | +app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax }); |
| 62 | + |
| 63 | +app.UseStaticFiles(); |
| 64 | +app.UseSession(); |
| 65 | +app.UseRouting(); |
| 66 | +app.UseAuthentication(); |
| 67 | +app.UseAuthorization(); |
| 68 | +app.MapDefaultControllerRoute(); |
| 69 | +app.MapRazorPages(); |
| 70 | + |
| 71 | +await app.RunAsync(); |
0 commit comments