Exemple #1
0
 /// <summary>
 /// Instantiates a new <see cref="CorsMiddleware"/>.
 /// </summary>
 /// <param name="next">The next middleware in the pipeline.</param>
 /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
 /// <param name="loggerFactory">An instance of <see cref="ILoggerFactory"/>.</param>
 public CorsMiddleware(
     RequestDelegate next,
     ICorsService corsService,
     ILoggerFactory loggerFactory)
     : this(next, corsService, loggerFactory, policyName : null)
 {
 }
Exemple #2
0
        /// <summary>
        /// Instantiates a new <see cref="CorsMiddleware"/>.
        /// </summary>
        /// <param name="next">The next middleware in the pipeline.</param>
        /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
        /// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
        /// <param name="policyName">An optional name of the policy to be fetched.</param>
        public CorsMiddleware(
            RequestDelegate next,
            ICorsService corsService,
            ICorsPolicyProvider policyProvider,
            string policyName)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (corsService == null)
            {
                throw new ArgumentNullException(nameof(corsService));
            }

            if (policyProvider == null)
            {
                throw new ArgumentNullException(nameof(policyProvider));
            }

            _next               = next;
            _corsService        = corsService;
            _corsPolicyProvider = policyProvider;
            _corsPolicyName     = policyName;
        }
Exemple #3
0
        /// <summary>
        /// Instantiates a new <see cref="CorsMiddleware"/>.
        /// </summary>
        /// <param name="next">The next middleware in the pipeline.</param>
        /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
        /// <param name="policy">An instance of the <see cref="CorsPolicy"/> which can be applied.</param>
        /// <param name="loggerFactory">An instance of <see cref="ILoggerFactory"/>.</param>
        public CorsMiddleware(
            RequestDelegate next,
            ICorsService corsService,
            CorsPolicy policy,
            ILoggerFactory loggerFactory)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (corsService == null)
            {
                throw new ArgumentNullException(nameof(corsService));
            }

            if (policy == null)
            {
                throw new ArgumentNullException(nameof(policy));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _next       = next;
            CorsService = corsService;
            _policy     = policy;
            Logger      = loggerFactory.CreateLogger <CorsMiddleware>();
        }
Exemple #4
0
        public async Task Invoke(HttpContext context,
                                 ICorsService corsService,
                                 ICorsPolicyProvider policyProvider,
                                 IAllowedOriginsProvider allowedOriginsProvider)
        {
            if (context.Request.Headers.ContainsKey(CorsConstants.Origin))
            {
                var accessControlRequestMethod = context.Request.Headers[CorsConstants.AccessControlRequestMethod];
                var isPreflight = IsPreflight(context, accessControlRequestMethod);

                var corsPolicy = await GetPolicy(context, isPreflight, policyProvider, allowedOriginsProvider);

                if (corsPolicy != null)
                {
                    var corsResult = corsService.EvaluatePolicy(context, corsPolicy);
                    corsService.ApplyResult(corsResult, context.Response);

                    if (isPreflight)
                    {
                        context.Response.StatusCode = StatusCodes.Status204NoContent;
                        return;
                    }
                }
            }

            await Next(context);
        }
        /// <summary>
        /// Instantiates a new <see cref="CorsMiddleware"/>.
        /// </summary>
        /// <param name="next">The next middleware in the pipeline.</param>
        /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
        /// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
        /// <param name="loggerFactory">An instance of <see cref="ILoggerFactory"/>.</param>
        /// <param name="policyName">An optional name of the policy to be fetched.</param>
        public CorsMiddleware(
            RequestDelegate next,
            ICorsService corsService,
            ICorsPolicyProvider policyProvider,
            ILoggerFactory loggerFactory,
            string policyName)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (corsService == null)
            {
                throw new ArgumentNullException(nameof(corsService));
            }

            if (policyProvider == null)
            {
                throw new ArgumentNullException(nameof(policyProvider));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _next               = next;
            CorsService         = corsService;
            _corsPolicyProvider = policyProvider;
            _corsPolicyName     = policyName;
            Logger              = loggerFactory.CreateLogger <CorsMiddleware>();
        }
 public CorsMiddleware(
     RequestDelegate next,
     ICorsService corsService,
     ICorsPolicyProvider policyProvider)
     : this(next, corsService, policyProvider, NullLoggerFactory.Instance, policyName : null)
 {
 }
 public CorsMiddleware(
     RequestDelegate next,
     ICorsService corsService,
     CorsPolicy policy)
     : this(next, corsService, policy, NullLoggerFactory.Instance)
 {
 }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ICorsService corsService, ICorsPolicyProvider corsPolicyProvider)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors("CorsPolicy");

            // To serve PBF Files, we need to allow unknown filetypes
            // to be served by the Webserver:
            app.UseStaticFiles(new StaticFileOptions
            {
                ServeUnknownFileTypes = true,
                OnPrepareResponse     = (ctx) =>
                {
                    var policy = corsPolicyProvider.GetPolicyAsync(ctx.Context, "CorsPolicy")
                                 .ConfigureAwait(false)
                                 .GetAwaiter().GetResult();

                    var corsResult = corsService.EvaluatePolicy(ctx.Context, policy);

                    corsService.ApplyResult(corsResult, ctx.Context.Response);
                }
            });

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute().RequireCors("CorsPolicy");
            });
        }
Exemple #9
0
        /// <summary>
        /// Instantiates a new <see cref="CorsMiddleware"/>.
        /// </summary>
        /// <param name="next">The next middleware in the pipeline.</param>
        /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
        /// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
        /// <param name="policyName">An optional name of the policy to be fetched.</param>
        public CorsMiddleware(
            RequestDelegate next,
            ICorsService corsService,
            ICorsPolicyProvider policyProvider,
            string policyName)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (corsService == null)
            {
                throw new ArgumentNullException(nameof(corsService));
            }

            if (policyProvider == null)
            {
                throw new ArgumentNullException(nameof(policyProvider));
            }

            _next = next;
            _corsService = corsService;
            _corsPolicyProvider = policyProvider;
            _corsPolicyName = policyName;
        }
Exemple #10
0
 /// <summary>
 /// Instantiates a new <see cref="CorsMiddleware"/>.
 /// </summary>
 /// <param name="next">The next middleware in the pipeline.</param>
 /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
 /// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
 public CorsMiddleware(
     RequestDelegate next,
     ICorsService corsService,
     ICorsPolicyProvider policyProvider)
     : this(next, corsService, policyProvider, policyName : null)
 {
 }
Exemple #11
0
 public CorsMiddlewar(RequestDelegate next,
                      ICorsService corsService,
                      ICorsPolicyProvider corsPolicyProvider)
 {
     _next               = next;
     _corsService        = corsService;
     _corsPolicyProvider = corsPolicyProvider;
 }
Exemple #12
0
 public CorsMiddleWare(RequestDelegate next, ICorsService service, ICorsPolicyProvider corsPolicyProvider, CorsPolicy policy, string corsPolicyName)
 {
     _next               = next;
     _corsService        = service;
     _corsPolicyProvider = corsPolicyProvider;
     _policy             = policy;
     _corsPolicyName     = corsPolicyName;
 }
 public ExceptionsHandlingMiddleware(RequestDelegate next, ILogger <ExceptionsHandlingMiddleware> logger,
                                     ICorsService corsService, IOptions <CorsOptions> corsOptions)
 {
     _next        = next;
     _logger      = logger;
     _corsService = corsService;
     _corsOptions = corsOptions.Value;
 }
 /// <summary>
 /// DynamicCorsMiddleware
 /// </summary>
 public DynamicCorsMiddleware(RequestDelegate next,
                              ILogger <DynamicCorsMiddleware> logger,
                              ICorsService corsService)
 {
     this.logger      = logger;
     this.next        = next;
     this.corsService = corsService;
 }
Exemple #15
0
 /// <summary>
 /// Instantiates a new <see cref="CorsMiddleware"/>.
 /// </summary>
 /// <param name="next">The next middleware in the pipeline.</param>
 /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
 /// <param name="policy">An instance of the <see cref="CorsPolicy"/> which can be applied.</param>
 public CorsMiddleware(
    [NotNull] RequestDelegate next,
    [NotNull] ICorsService corsService,
    [NotNull] CorsPolicy policy)
 {
     _next = next;
     _corsService = corsService;
     _policy = policy;
 }
Exemple #16
0
 /// <summary>
 /// Instantiates a new <see cref="CorsMiddleware"/>.
 /// </summary>
 /// <param name="next">The next middleware in the pipeline.</param>
 /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
 /// <param name="policy">An instance of the <see cref="CorsPolicy"/> which can be applied.</param>
 public CorsMiddleware(
     [NotNull] RequestDelegate next,
     [NotNull] ICorsService corsService,
     [NotNull] CorsPolicy policy)
 {
     _next        = next;
     _corsService = corsService;
     _policy      = policy;
 }
Exemple #17
0
 /// <summary>
 /// Instantiates a new <see cref="CorsMiddleware"/>.
 /// </summary>
 /// <param name="next">The next middleware in the pipeline.</param>
 /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
 /// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
 /// <param name="policyName">An optional name of the policy to be fetched.</param>
 public CorsMiddleware(
     [NotNull] RequestDelegate next,
     [NotNull] ICorsService corsService,
     [NotNull] ICorsPolicyProvider policyProvider,
     string policyName)
 {
     _next               = next;
     _corsService        = corsService;
     _corsPolicyProvider = policyProvider;
     _corsPolicyName     = policyName;
 }
Exemple #18
0
 /// <summary>
 /// Instantiates a new <see cref="CorsMiddleware"/>.
 /// </summary>
 /// <param name="next">The next middleware in the pipeline.</param>
 /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
 /// <param name="policyProvider">A policy provider which can get an <see cref="CorsPolicy"/>.</param>
 /// <param name="policyName">An optional name of the policy to be fetched.</param>
 public CorsMiddleware(
     [NotNull] RequestDelegate next,
     [NotNull] ICorsService corsService,
     [NotNull] ICorsPolicyProvider policyProvider,
     string policyName)
 {
     _next = next;
     _corsService = corsService;
     _corsPolicyProvider = policyProvider;
     _corsPolicyName = policyName;
 }
        private CorsAuthorizationFilter GetFilter(ICorsService corsService)
        {
            var policyProvider = new Mock <ICorsPolicyProvider>();

            policyProvider
            .Setup(o => o.GetPolicyAsync(It.IsAny <HttpContext>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new CorsPolicy()));

            return(new CorsAuthorizationFilter(corsService, policyProvider.Object, Mock.Of <ILoggerFactory>())
            {
                PolicyName = string.Empty
            });
        }
Exemple #20
0
        /// <summary>
        /// Instantiates a new <see cref="CorsMiddleware"/>.
        /// </summary>
        /// <param name="next">The next middleware in the pipeline.</param>
        /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
        /// <param name="policy">An instance of the <see cref="CorsPolicy"/> which can be applied.</param>
        public CorsMiddleware(
            RequestDelegate next,
            ICorsService corsService,
            CorsPolicy policy)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (corsService == null)
            {
                throw new ArgumentNullException(nameof(corsService));
            }

            if (policy == null)
            {
                throw new ArgumentNullException(nameof(policy));
            }

            _next        = next;
            _corsService = corsService;
            _policy      = policy;
        }
Exemple #21
0
        /// <summary>
        /// Creates a new instance of <see cref="CorsAuthorizationFilter"/>.
        /// </summary>
        /// <param name="corsService">The <see cref="ICorsService"/>.</param>
        /// <param name="policyProvider">The <see cref="ICorsPolicyProvider"/>.</param>
        /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
        public CorsAuthorizationFilter(
            ICorsService corsService,
            ICorsPolicyProvider policyProvider,
            ILoggerFactory loggerFactory)
        {
            if (corsService == null)
            {
                throw new ArgumentNullException(nameof(corsService));
            }

            if (policyProvider == null)
            {
                throw new ArgumentNullException(nameof(policyProvider));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _corsService        = corsService;
            _corsPolicyProvider = policyProvider;
            _logger             = loggerFactory.CreateLogger(GetType());
        }
Exemple #22
0
        /// <summary>
        /// Instantiates a new <see cref="CorsMiddleware"/>.
        /// </summary>
        /// <param name="next">The next middleware in the pipeline.</param>
        /// <param name="corsService">An instance of <see cref="ICorsService"/>.</param>
        /// <param name="policy">An instance of the <see cref="CorsPolicy"/> which can be applied.</param>
        public CorsMiddleware(
           RequestDelegate next,
           ICorsService corsService,
           CorsPolicy policy)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (corsService == null)
            {
                throw new ArgumentNullException(nameof(corsService));
            }

            if (policy == null)
            {
                throw new ArgumentNullException(nameof(policy));
            }

            _next = next;
            _corsService = corsService;
            _policy = policy;
        }
Exemple #23
0
 public CorsApplyingMiddleware(RequestDelegate next, ICorsService corsService)
 {
     _corsService = corsService;
     _next        = next;
 }
 public CorsInspectionMiddleware(RequestDelegate next, ICorsService service, ILogger <CorsInspectionMiddleware> logger)
 {
     _next    = next;
     _service = service;
     _logger  = logger;
 }
        private CorsAuthorizationFilter GetFilter(ICorsService corsService)
        {
            var policyProvider = new Mock<ICorsPolicyProvider>();
            policyProvider
                .Setup(o => o.GetPolicyAsync(It.IsAny<HttpContext>(), It.IsAny<string>()))
                .Returns(Task.FromResult(new CorsPolicy()));

            return new CorsAuthorizationFilter(corsService, policyProvider.Object)
            {
                PolicyName = string.Empty
            };
        }
Exemple #26
0
 /// <summary>
 /// Creates a new instance of <see cref="CorsAuthorizationFilter"/>.
 /// </summary>
 /// <param name="corsService">The <see cref="ICorsService"/>.</param>
 /// <param name="policyProvider">The <see cref="ICorsPolicyProvider"/>.</param>
 public CorsAuthorizationFilter(ICorsService corsService, ICorsPolicyProvider policyProvider)
     : this(corsService, policyProvider, NullLoggerFactory.Instance)
 {
 }
Exemple #27
0
 /// <summary>
 /// Creates a new instace of <see cref="CorsAuthorizationFilter"/>.
 /// </summary>
 /// <param name="corsService">The <see cref="ICorsService"/>.</param>
 /// <param name="policyProvider">The <see cref="ICorsPolicyProvider"/>.</param>
 public CorsAuthorizationFilter(ICorsService corsService, ICorsPolicyProvider policyProvider)
 {
     _corsService        = corsService;
     _corsPolicyProvider = policyProvider;
 }
 /// <summary>
 /// Creates a new instance of <see cref="CorsAuthorizationFilter"/>.
 /// </summary>
 /// <param name="corsService">The <see cref="ICorsService"/>.</param>
 /// <param name="policyProvider">The <see cref="ICorsPolicyProvider"/>.</param>
 public CorsAuthorizationFilter(ICorsService corsService, ICorsPolicyProvider policyProvider)
 {
     _corsService = corsService;
     _corsPolicyProvider = policyProvider;
 }