/// <summary> /// Add services needed to support CORS to the given <paramref name="serviceCollection"/>. /// </summary> /// <param name="serviceCollection">The service collection to which CORS services are added.</param> /// <param name="configure">A delegate which is run to configure the services.</param> /// <returns>The updated <see cref="IServiceCollection"/>.</returns> public static IServiceCollection AddCors( this IServiceCollection serviceCollection, Action<CorsOptions> configure) { if (serviceCollection == null) { throw new ArgumentNullException(nameof(serviceCollection)); } if (configure == null) { throw new ArgumentNullException(nameof(configure)); } serviceCollection.Configure(configure); return serviceCollection.AddCors(); }
/// <summary> /// Add cross-origin resource sharing (CORS) services and configures named CORS policies. See /// https://docs.asp.net/en/latest/security/cors.html /// </summary> /// <param name="services">The services collection or IoC container.</param> public static IServiceCollection AddCorsPolicies(this IServiceCollection services) { return services.AddCors( options => { options.AddPolicy( options.DefaultPolicyName, x => { x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials(); }); options.AddPolicy("MyCustomPolicy", x => {}); }); }
public static IServiceCollection AddHostServices(this IServiceCollection services) { services.TryAddTransient<SessionCookie>(); services.TryAddTransient<ClientListCookie>(); services.TryAddTransient(typeof(MessageCookie<>)); services.TryAddTransient<SignInInteraction>(); services.TryAddTransient<SignOutInteraction>(); services.TryAddTransient<ConsentInteraction>(); services.TryAddTransient<ErrorInteraction>(); services.AddTransient<ICorsPolicyProvider>(provider=> { return new PolicyProvider( provider.GetRequiredService<ILogger<PolicyProvider>>(), Constants.RoutePaths.CorsPaths, provider.GetRequiredService<ICorsPolicyService>()); }); services.AddCors(); return services; }