/// <summary> /// Add cross-origin resource sharing (CORS) services and configures named CORS policies. See /// https://docs.asp.net/en/latest/security/cors.html /// </summary> public static IMvcCoreBuilder AddCustomCors(this IMvcCoreBuilder builder) => builder.AddCors( options => // Create named CORS policies here which you can consume using application.UseCors("PolicyName") // or a [EnableCors("PolicyName")] attribute on your controller or action. options.AddPolicy( CorsPolicyName.AllowAny, x => x .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()));
private static IMvcBuilder AddWebApi(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } IMvcCoreBuilder builder = services.AddMvcCore(); builder.AddJsonFormatters(); builder.AddApiExplorer(); builder.AddCors(); return(new MvcBuilder(builder.Services, builder.PartManager)); }
/// <summary> /// Add cross-origin resource sharing (CORS) services and configures named CORS policies. See /// https://docs.asp.net/en/latest/security/cors.html /// </summary> public static IMvcCoreBuilder AddCustomCors(this IMvcCoreBuilder builder, IConfiguration configuration) => builder.AddCors( options => { // Create named CORS policies here which you can consume using application.UseCors("PolicyName") // or a [EnableCors("PolicyName")] attribute on your controller or action. var corsOptions = configuration.GetSection("Cors").Get <CorsOptions>(); options.AddPolicy( CorsPolicyName.AllowAngularApp, x => x .AllowCredentials() .WithOrigins(corsOptions.AllowedOrigins) .SetIsOriginAllowedToAllowWildcardSubdomains() .WithMethods("HEAD", "GET", "POST") .WithHeaders("Accept", "Authorization", "Content-Type")); });
/// <summary> /// Adds WebApi services to the specified <see cref="IServiceCollection" />. /// </summary> /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> /// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the WebApi services.</returns> public static IMvcBuilder AddWebApi(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException("services"); } IMvcCoreBuilder mvcCoreBuilder = services.AddMvcCore(); mvcCoreBuilder.AddApiExplorer(); mvcCoreBuilder.AddAuthorization(); mvcCoreBuilder.AddFormatterMappings(); mvcCoreBuilder.AddDataAnnotations(); mvcCoreBuilder.AddJsonFormatters(); mvcCoreBuilder.AddCors(); //-MvcServiceCollectionExtensions.AddDefaultFrameworkParts(mvcCoreBuilder.PartManager); //-mvcCoreBuilder.AddViews(); //-mvcCoreBuilder.AddRazorViewEngine(); //-mvcCoreBuilder.AddRazorPages(); //-mvcCoreBuilder.AddCacheTagHelper(); return(new MvcBuilder(mvcCoreBuilder.Services, mvcCoreBuilder.PartManager)); }
public static IMvcCoreBuilder AddDefaultCors([NotNull] this IMvcCoreBuilder thisValue, params string[] origins) { thisValue.AddCors(options => options.AddDefaultPolicy(origins)); return(thisValue); }