private void GetInitCORSPolicy(IConfiguration configuration) { try { this.CORSPolicySettings = configuration.GetSection("CORSPolicy").Get <CORSPolicySettings>(); } catch (Exception ex) { Console.WriteLine("Unable to set CORS policy --", ex.ToString()); } }
internal static void AddCors(this IServiceCollection services, ICORSPolicySettings corsPolicySettings) { services.AddCors(options => { options.AddPolicy(name: corsPolicySettings.PolicyName, builder => { builder.WithOrigins(corsPolicySettings.AllowedOrigins); // https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api // If you set headers to anything other than "*", // you should include at least "accept", "content-type", and "origin", // plus any custom headers that you want to support builder.WithHeaders(HeaderNames.Accept, HeaderNames.ContentType, HeaderNames.Origin); builder.WithMethods ( HttpMethod.Get.Method, HttpMethod.Post.Method, HttpMethod.Patch.Method, HttpMethod.Delete.Method ); }); }); }