| | | 1 | | using Anichron.Infrastructure.Configuration; |
| | | 2 | | using Microsoft.AspNetCore.Cors.Infrastructure; |
| | | 3 | | |
| | | 4 | | namespace Anichron.API.Infrastructure; |
| | | 5 | | |
| | | 6 | | internal static class CorsOrigins |
| | | 7 | | { |
| | | 8 | | internal static string[] Read(IConfiguration configuration) |
| | 81 | 9 | | => [.. ConfigurationList.Parse(configuration["Cors:AllowedOrigins"], static origin => origin)]; |
| | | 10 | | |
| | | 11 | | // With no configured origins the default policy allows nothing (same-origin behavior). |
| | | 12 | | internal static void ConfigureDefaultPolicy(CorsOptions options, string[] allowedOrigins) |
| | 5 | 13 | | { |
| | 5 | 14 | | if (allowedOrigins.Length > 0) |
| | 2 | 15 | | { |
| | 2 | 16 | | options.AddDefaultPolicy(policy => |
| | 2 | 17 | | policy.WithOrigins(allowedOrigins) |
| | 2 | 18 | | .AllowAnyHeader() |
| | 2 | 19 | | .AllowAnyMethod() |
| | 2 | 20 | | .AllowCredentials()); |
| | 2 | 21 | | } |
| | 5 | 22 | | } |
| | | 23 | | |
| | | 24 | | // SameSite=None is required when the UI and API are on different origins so browsers |
| | | 25 | | // send the cookie on cross-origin requests. SameSite=Strict is safer for same-origin. |
| | | 26 | | internal static SameSiteMode RefreshCookieSameSite(string[] allowedOrigins) |
| | 77 | 27 | | => allowedOrigins.Length > 0 ? SameSiteMode.None : SameSiteMode.Strict; |
| | | 28 | | } |