/// <summary>
        /// Constructor
        /// </summary>
        /// <param name="options">The CORS options to be applied</param>
        public CorsBasedSecurityMessageHandler(
            CorsOptions options = null)
        {
            options.NotNull(nameof(options));

            Options = options;
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="options">The CORS options to be applied</param>
        public CorsBasedSecurityMessageHandler(
            CorsOptions options = null)
        {
            options.NotNull(nameof(options));

            Options = options;
        }
 /// <summary>
 /// Configure message handlers to enable CORS based SecurityMessageHandler for all the requests
 /// </summary>
 /// <param name="config">The config</param>
 /// <param name="options">The CORS options to be applied</param>
 public static void EnableCors(
     this HttpConfiguration config,
     CorsOptions options)
 {
     config.MessageHandlers.Add(
         new CorsBasedSecurityMessageHandler(options));
 }
 public static IDictionary<string, string> GetCorsResponseHeaders(
     this CorsEngine corsEngine,
     CorsRequestContext context,
     CorsOptions options)
 {
     return corsEngine
         .EvaluatePolicy(context, options.GetCorsPolicy())
         .ToResponseHeaders() ?? new Dictionary<string, string>();
 }
 public static IDictionary <string, string> GetCorsResponseHeaders(
     this CorsEngine corsEngine,
     CorsRequestContext context,
     CorsOptions options)
 {
     return(corsEngine
            .EvaluatePolicy(context, options.GetCorsPolicy())
            .ToResponseHeaders() ?? new Dictionary <string, string>());
 }
        public static bool EvaluateCorsPolicy(
            this CorsEngine corsEngine,
            CorsRequestContext context,
            CorsOptions options)
        {
            var result = corsEngine.EvaluatePolicy(context, options.GetCorsPolicy());

            return result.IsNotNull() && result.IsValid;
        }
        public static bool EvaluateCorsPolicy(
            this CorsEngine corsEngine,
            CorsRequestContext context,
            CorsOptions options)
        {
            var result = corsEngine.EvaluatePolicy(context, options.GetCorsPolicy());

            return(result.IsNotNull() && result.IsValid);
        }
Esempio n. 8
0
        private static void SetOriginsIfRequired(
            this CorsOptions options,
            CorsPolicy corsPolicy)
        {
            if (options.AllowAnyOrigin)
            {
                return;
            }

            SetList(options.AllowedOrigins, corsPolicy.Origins);
        }
Esempio n. 9
0
        private static void SetMethodsIfRequired(
            this CorsOptions options,
            CorsPolicy corsPolicy)
        {
            if (options.AllowAnyMethod)
            {
                return;
            }

            SetList(options.AllowedMethods, corsPolicy.Methods);
        }
Esempio n. 10
0
        private static void SetHeadersIfRequired(
            this CorsOptions options,
            CorsPolicy corsPolicy)
        {
            if (options.AllowAnyHeader)
            {
                return;
            }

            SetList(options.AllowedHeaders, corsPolicy.Headers);
        }
Esempio n. 11
0
        public static CorsPolicy GetCorsPolicy(
            this CorsOptions options)
        {
            options.NotNull(nameof(options));

            var corsPolicy = new CorsPolicy
            {
                AllowAnyHeader      = options.AllowAnyHeader,
                AllowAnyMethod      = options.AllowAnyMethod,
                AllowAnyOrigin      = options.AllowAnyOrigin,
                SupportsCredentials = options.SupportsCredentials,
            };

            if (options.PreflightMaxAge.HasValue)
            {
                corsPolicy.PreflightMaxAge = options.PreflightMaxAge;
            }

            options.SetHeadersIfRequired(corsPolicy);
            options.SetMethodsIfRequired(corsPolicy);
            options.SetOriginsIfRequired(corsPolicy);

            return(corsPolicy);
        }