Example #1
0
        private void EnableCors(CloudBlobClient blobClient)
        {
            try
            {
                CorsHttpMethods allowedMethods = CorsHttpMethods.None;
                allowedMethods = allowedMethods | CorsHttpMethods.Get;
                allowedMethods = allowedMethods | CorsHttpMethods.Put;
                allowedMethods = allowedMethods | CorsHttpMethods.Post;
                allowedMethods = allowedMethods | CorsHttpMethods.Delete;
                allowedMethods = allowedMethods | CorsHttpMethods.Options;

                var          delimiter      = new[] { "," };
                CorsRule     corsRule       = new CorsRule();
                const string allowedOrigins = "*";
                const string allowedHeaders = "*";
                const string exposedHeaders = "";

                string[] allAllowedOrigin = allowedOrigins.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                string[] allExpHeaders    = exposedHeaders.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                string[] allAllowHeaders  = allowedHeaders.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);

                List <string> corsAllowedOrigin = new List <string>();
                foreach (var item in allAllowedOrigin)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        corsAllowedOrigin.Add(item.Trim());
                    }
                }
                List <string> corsExposedHeaders = new List <string>();
                foreach (var item in allExpHeaders)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        corsExposedHeaders.Add(item.Trim());
                    }
                }
                List <string> corsAllowHeaders = new List <string>();
                foreach (var item in allAllowHeaders)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        corsAllowHeaders.Add(item.Trim());
                    }
                }
                corsRule.MaxAgeInSeconds = 200;
                corsRule.AllowedMethods  = allowedMethods;
                corsRule.AllowedHeaders  = corsAllowHeaders;
                corsRule.AllowedOrigins  = corsAllowedOrigin;
                corsRule.ExposedHeaders  = corsExposedHeaders;
                ServiceProperties properties = blobClient.GetServiceProperties();
                properties.Cors.CorsRules.Clear();
                properties.Cors.CorsRules.Add(corsRule);
                blobClient.SetServiceProperties(properties);
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error enable CORS", ex);
            }
        }
Example #2
0
        /// <summary>
        /// Parse CorsHttpMethods from XSCL to String Array
        /// </summary>
        /// <param name="methods">CorsHttpMethods from XSCL</param>
        /// <returns>String Array</returns>
        private static string[] ConvertCorsHttpMethodToString(CorsHttpMethods methods)
        {
            List <string> methodList = new List <string>();

            foreach (CorsHttpMethods methodValue in Enum.GetValues(typeof(CorsHttpMethods)).Cast <CorsHttpMethods>())
            {
                if (methodValue != CorsHttpMethods.None && (methods & methodValue) != 0)
                {
                    methodList.Add(methodValue.ToString());
                }
            }

            return(methodList.ToArray());
        }
        private string[] ConvertCorsHttpMethodToString(CorsHttpMethods methods)
        {
            List<string> methodList = new List<string>();

            foreach (CorsHttpMethods methodValue in Enum.GetValues(typeof(CorsHttpMethods)).Cast<CorsHttpMethods>())
            {
                if (methodValue != CorsHttpMethods.None && (methods & methodValue) != 0)
                {
                    methodList.Add(methodValue.ToString());
                }
            }

            return methodList.ToArray();
        }
        private CorsHttpMethods MapAllowedMethods(IList <string> allowedMethods)
        {
            var corsHttpMethods = new CorsHttpMethods();

            foreach (var allowedMethod in allowedMethods)
            {
                var tmpMethod = new CorsHttpMethods();
                if (Enum.TryParse <CorsHttpMethods>(allowedMethod, true, out tmpMethod))
                {
                    corsHttpMethods |= tmpMethod;
                }
            }

            return(corsHttpMethods);
        }
Example #5
0
        private static CorsHttpMethods ExpandCorsHttpMethods(List <string> methods)
        {
            CorsHttpMethods allowedmethods = CorsHttpMethods.None;

            methods.All(method =>
            {
                switch (method)
                {
                case "DELETE":
                    allowedmethods |= CorsHttpMethods.Delete;
                    return(true);

                case "GET":
                    allowedmethods |= CorsHttpMethods.Get;
                    return(true);

                case "HEAD":
                    allowedmethods |= CorsHttpMethods.Head;
                    return(true);

                case "OPTIONS":
                    allowedmethods |= CorsHttpMethods.Options;
                    return(true);

                case "POST":
                    allowedmethods |= CorsHttpMethods.Post;
                    return(true);

                case "PUT":
                    allowedmethods |= CorsHttpMethods.Put;
                    return(true);

                case "TRACE":
                    allowedmethods |= CorsHttpMethods.Trace;
                    return(true);

                default:
                    allowedmethods = CorsHttpMethods.None;
                    return(true);
                }
            });

            return(allowedmethods);
        }
Example #6
0
        public static string[] GetRandomMethods()
        {
            // In MSDN, it says it only supports allowed methods: Supported methods are DELETE, GET, HEAD, MERGE, POST, OPTIONS and PUT
            // https://msdn.microsoft.com/en-us/library/azure/dn535601.aspx
            // If it is not changed, adding "CorsHttpMethods.Get | <random>" to make sure at least one method is valid.
            CorsHttpMethods methods = CorsHttpMethods.Get | (CorsHttpMethods)rd.Next(1, 512);

            List <string> methodList = new List <string>();

            foreach (CorsHttpMethods methodValue in Enum.GetValues(typeof(CorsHttpMethods)).Cast <CorsHttpMethods>())
            {
                if ((methods & methodValue) != 0)
                {
                    methodList.Add(RandomStringCase(methodValue.ToString()));
                }
            }

            return(methodList.ToArray());
        }
Example #7
0
        /// <summary>
        /// Set Allowed method for a Cors Rule from a string array
        /// </summary>
        private void SetAllowedMethods(CorsRule corsRule, string[] allowedMethods)
        {
            corsRule.AllowedMethods = CorsHttpMethods.None;

            if (null != allowedMethods)
            {
                foreach (var method in allowedMethods)
                {
                    CorsHttpMethods allowedCorsMethod = CorsHttpMethods.None;
                    if (Enum.TryParse <CorsHttpMethods>(method, true, out allowedCorsMethod))
                    {
                        corsRule.AllowedMethods |= allowedCorsMethod;
                    }
                    else
                    {
                        Test.Error(string.Format("Can't parse {0} to CorsHttpMethods.", method));
                    }
                }
            }
        }
        /// <summary>
        /// Comapare PSCorsRule and CorsProperties, to make sure they are same content
        /// </summary>
        static private void CompareCors(PSCorsRule[] psCorsRules, CorsProperties corsProperties)
        {
            if ((psCorsRules == null || psCorsRules.Length == 0) &&
                (corsProperties == null || corsProperties.CorsRules == null || corsProperties.CorsRules.Count == 0))
            {
                return;
            }
            Assert.AreEqual(psCorsRules.Length, corsProperties.CorsRules.Count);
            int i = 0;

            foreach (CorsRule CorsRule in corsProperties.CorsRules)
            {
                PSCorsRule psCorsRule = psCorsRules[i];
                i++;
                CompareStrings(psCorsRule.AllowedHeaders, CorsRule.AllowedHeaders);
                CompareStrings(psCorsRule.ExposedHeaders, CorsRule.ExposedHeaders);
                CompareStrings(psCorsRule.AllowedOrigins, CorsRule.AllowedOrigins);
                Assert.AreEqual(psCorsRule.MaxAgeInSeconds, CorsRule.MaxAgeInSeconds);

                CorsHttpMethods psAllowedMethods = CorsHttpMethods.None;
                foreach (string method in psCorsRule.AllowedMethods)
                {
                    CorsHttpMethods allowedCorsMethod = CorsHttpMethods.None;
                    if (Enum.TryParse <CorsHttpMethods>(method, true, out allowedCorsMethod))
                    {
                        psAllowedMethods |= allowedCorsMethod;
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format("Can't parse {0} to CorsHttpMethods.", method));
                    }
                }

                Assert.AreEqual(psAllowedMethods, CorsRule.AllowedMethods);
            }
        }
Example #9
0
        private static CorsRule FindBestMatchingRule(ServiceProperties serviceProperties, IEnumerable<string> requiredHeaders, CorsHttpMethods requiredMethods, IEnumerable<string> origins) {
            var query =
                from rule in serviceProperties.Cors.CorsRules
                let hasRequiredHeadersAndMethods = (rule.AllowedHeaders.Contains("*") || requiredHeaders.All(rule.AllowedHeaders.Contains)) && (rule.AllowedMethods & requiredMethods) == requiredMethods
                let numberMatchingOrigins = origins.Count(x => rule.AllowedOrigins.Contains(x))
                where hasRequiredHeadersAndMethods
                orderby hasRequiredHeadersAndMethods descending, numberMatchingOrigins descending
                select rule;

            return query.FirstOrDefault();
        }
Example #10
0
        public async Task EnableCORSAsync()
        {
            CorsHttpMethods allowedMethods = CorsHttpMethods.None;

            allowedMethods |= CorsHttpMethods.Get;
            allowedMethods |= CorsHttpMethods.Put;
            allowedMethods |= CorsHttpMethods.Post;
            allowedMethods |= CorsHttpMethods.Delete;
            allowedMethods |= CorsHttpMethods.Options;

            var          delimiter      = new[] { "," };
            var          corsRule       = new CorsRule();
            const string AllowedOrigins = "*";
            const string AllowedHeaders = "*";
            const string ExposedHeaders = "";

            var allAllowedOrigin = AllowedOrigins.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
            var allExpHeaders    = ExposedHeaders.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
            var allAllowHeaders  = AllowedHeaders.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);

            var corsAllowedOrigin = new List <string>();

            foreach (var item in allAllowedOrigin)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    corsAllowedOrigin.Add(item.Trim());
                }
            }

            var corsExposedHeaders = new List <string>();

            foreach (var item in allExpHeaders)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    corsExposedHeaders.Add(item.Trim());
                }
            }

            var corsAllowHeaders = new List <string>();

            foreach (var item in allAllowHeaders)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    corsAllowHeaders.Add(item.Trim());
                }
            }

            corsRule.MaxAgeInSeconds = 200;
            corsRule.AllowedMethods  = allowedMethods;
            corsRule.AllowedHeaders  = corsAllowHeaders;
            corsRule.AllowedOrigins  = corsAllowedOrigin;
            corsRule.ExposedHeaders  = corsExposedHeaders;
            var serviceProperties = await this.blobClient.GetServicePropertiesAsync();

            serviceProperties.Cors.CorsRules.Clear();
            serviceProperties.Cors.CorsRules.Add(corsRule);
            await this.blobClient.SetServicePropertiesAsync(serviceProperties);
        }
        private CorsHttpMethods MapAllowedMethods(IList<string> allowedMethods)
        {
            var corsHttpMethods = new CorsHttpMethods();

            foreach (var allowedMethod in allowedMethods)
            {
                var tmpMethod = new CorsHttpMethods();
                if (Enum.TryParse<CorsHttpMethods>(allowedMethod, true, out tmpMethod))
                    corsHttpMethods |= tmpMethod;
            }

            return corsHttpMethods;
        }
 private IList<string> MapAllowedMethods(CorsHttpMethods allowedMethods)
 {
     return allowedMethods.ToString().Replace(" ", String.Empty).Split(',').ToList();
 }
        public string EnableStorageCORS(Account account)
        {
            try
            {
                var storageAccount = StorageUtils.CreateCloudStorageAccount(account);
                var blobClient     = storageAccount.CreateCloudBlobClient();

                CorsHttpMethods allowedMethods = CorsHttpMethods.None;
                allowedMethods = allowedMethods | CorsHttpMethods.Get;
                allowedMethods = allowedMethods | CorsHttpMethods.Put;
                allowedMethods = allowedMethods | CorsHttpMethods.Post;
                allowedMethods = allowedMethods | CorsHttpMethods.Delete;
                allowedMethods = allowedMethods | CorsHttpMethods.Options;

                var          delimiter      = new[] { "," };
                CorsRule     corsRule       = new CorsRule();
                const string allowedOrigins = "*";
                const string allowedHeaders = "*";
                const string exposedHeaders = "";

                string[] allAllowedOrigin = allowedOrigins.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                string[] allExpHeaders    = exposedHeaders.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                string[] allAllowHeaders  = allowedHeaders.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);

                List <string> corsAllowedOrigin = new List <string>();
                foreach (var item in allAllowedOrigin)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        corsAllowedOrigin.Add(item.Trim());
                    }
                }
                List <string> corsExposedHeaders = new List <string>();
                foreach (var item in allExpHeaders)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        corsExposedHeaders.Add(item.Trim());
                    }
                }
                List <string> corsAllowHeaders = new List <string>();
                foreach (var item in allAllowHeaders)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        corsAllowHeaders.Add(item.Trim());
                    }
                }
                corsRule.MaxAgeInSeconds = 200;
                corsRule.AllowedMethods  = allowedMethods;
                corsRule.AllowedHeaders  = corsAllowHeaders;
                corsRule.AllowedOrigins  = corsAllowedOrigin;
                corsRule.ExposedHeaders  = corsExposedHeaders;
                ServiceProperties properties = blobClient.GetServiceProperties();
                properties.Cors.CorsRules.Clear();
                properties.Cors.CorsRules.Add(corsRule);
                blobClient.SetServiceProperties(properties);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return("Failed due to Incorrect Account Name or Key.");
            }
            return("Enabling CORS Succeed");
        }
 private IList <string> MapAllowedMethods(CorsHttpMethods allowedMethods)
 {
     return(allowedMethods.ToString().Replace(" ", String.Empty).Split(',').ToList());
 }
        private static CorsRule FindBestMatchingRule(ServiceProperties serviceProperties, IEnumerable <string> requiredHeaders, CorsHttpMethods requiredMethods, IEnumerable <string> origins)
        {
            var query =
                from rule in serviceProperties.Cors.CorsRules
                let hasRequiredHeadersAndMethods = (rule.AllowedHeaders.Contains("*") || requiredHeaders.All(rule.AllowedHeaders.Contains)) && (rule.AllowedMethods & requiredMethods) == requiredMethods
                                                   let numberMatchingOrigins = origins.Count(x => rule.AllowedOrigins.Contains(x))
                                                                               where hasRequiredHeadersAndMethods
                                                                               orderby hasRequiredHeadersAndMethods descending, numberMatchingOrigins descending
            select rule;

            return(query.FirstOrDefault());
        }