public static IServiceCollection AddCorsPolicy( this IServiceCollection services, string corsPolicy, string apiUrl) { var allowedOrigins = CorsHelper.GetAllowedOrigins(apiUrl); if (allowedOrigins != null) { services.AddCors(x => x.AddPolicy(corsPolicy, builder => builder.WithOrigins(allowedOrigins).AllowAnyHeader().AllowAnyMethod())); } services.AddTransient <ICorsPolicyAccessor, CorsPolicyAccessor>(); return(services); }
public async Task InvokeAsync(HttpContext context, ICorsPolicyAccessor accessor ) { var policy = accessor.GetPolicy(_policyName); string[] origins = CorsHelper.GetAllowedOrigins(_apiUrl); if (origins?.Length > 0) { var policyOrigins = policy.Origins; var newOrigins = origins.Except(policyOrigins); foreach (var origin in newOrigins) { policy.Origins.Add(origin); } } await _next(context); }