コード例 #1
0
ファイル: Program.cs プロジェクト: micajah/AzureFileService
        private static void DisplayCorsSettings(CorsProperties corsSettings)
        {
            if (corsSettings != null)
            {
                if (corsSettings != null)
                {
                    Console.WriteLine("Cors.CorsRules.Count         : " + corsSettings.CorsRules.Count);
                    for (int index = 0; index < corsSettings.CorsRules.Count; index++)
                    {
                        var corsRule = corsSettings.CorsRules[index];
                        Console.WriteLine("corsRule[index]              : " + index);
                        foreach (string allowedHeader in corsRule.AllowedHeaders)
                        {
                            Console.WriteLine("corsRule.AllowedHeaders      : " + allowedHeader);
                        }
                        Console.WriteLine("corsRule.AllowedMethods      : " + corsRule.AllowedMethods);

                        foreach (string allowedOrigins in corsRule.AllowedOrigins)
                        {
                            Console.WriteLine("corsRule.AllowedOrigins      : " + allowedOrigins);
                        }
                        foreach (string exposedHeaders in corsRule.ExposedHeaders)
                        {
                            Console.WriteLine("corsRule.ExposedHeaders      : " + exposedHeaders);
                        }
                        Console.WriteLine("corsRule.MaxAgeInSeconds     : " + corsRule.MaxAgeInSeconds);
                    }
                }
            }
        }
 private CorsProperties SetCorsRules(CorsProperties newProperties)
 {
     newProperties.CorsRules.Add(new Microsoft.WindowsAzure.Storage.Shared.Protocol.CorsRule()
     {
         AllowedHeaders = new System.Collections.Generic.List <string>()
         {
             "*"
         }, AllowedMethods = CorsHttpMethods.Get | CorsHttpMethods.Head
     });
     return(newProperties);
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: micajah/AzureFileService
        private static void AddRule(CorsProperties corsSettings)
        {
            CorsRule corsRule = new CorsRule()
            {
                AllowedHeaders = new List <string> {
                    "*"
                },
                AllowedMethods  = CorsHttpMethods.Put | CorsHttpMethods.Delete | CorsHttpMethods.Options,
                AllowedOrigins  = new List <string>(ConfigurationManager.AppSettings["mafs:AllowedOrigins"].Split(',')),
                MaxAgeInSeconds = int.MaxValue,
            };

            corsSettings.CorsRules.Add(corsRule);
        }
コード例 #4
0
        private ServiceProperties GetDefaultServiceProperties()
        {
            var Cors = new CorsProperties();

            Cors.CorsRules.Add(GetDefaultCorsRule());;

            return(new ServiceProperties()
            {
                Cors = Cors,
                DefaultServiceVersion = BlobDefaultServiceVersion,
                Logging = null,
                HourMetrics = null,
                MinuteMetrics = null,
            });
        }
コード例 #5
0
        public void ValidateParseCorsRulesTest()
        {
            CorsProperties coresproperties = PrepareCoresproperties();

            PSCorsRule[] pscores = PSCorsRule.ParseCorsRules(coresproperties);
            CompareCors(pscores, coresproperties);

            coresproperties = new CorsProperties();
            pscores         = PSCorsRule.ParseCorsRules(coresproperties);
            CompareCors(pscores, coresproperties);

            coresproperties = null;
            pscores         = PSCorsRule.ParseCorsRules(coresproperties);
            CompareCors(pscores, coresproperties);
        }
コード例 #6
0
ファイル: MemoryBlobStore.cs プロジェクト: sravan251/Azure
        private static CorsProperties Clone(CorsProperties cors)
        {
            if (cors == null)
            {
                return(null);
            }

            CorsProperties cloned = new CorsProperties();

            foreach (CorsRule rule in cors.CorsRules)
            {
                cloned.CorsRules.Add(Clone(rule));
            }

            return(cloned);
        }
コード例 #7
0
        private void ConfigureCors(CloudBlobClient cloudBlobClient)
        {
            ServiceProperties serviceProperties = cloudBlobClient.GetServiceProperties();

            CorsProperties corsProperties = new CorsProperties();

            corsProperties.CorsRules.Add(new CorsRule()
            {
                AllowedMethods  = CorsHttpMethods.Get,
                AllowedHeaders  = new string[] { "*" },
                AllowedOrigins  = new string[] { "*" },
                ExposedHeaders  = new string[] { "*" },
                MaxAgeInSeconds = 1800                 // 30 minutes
            });

            serviceProperties.Cors = corsProperties;

            cloudBlobClient.SetServiceProperties(serviceProperties);
        }
コード例 #8
0
        /// <summary>
        /// Parse Cors Rules from XSCL to PSCorsRule Array
        /// </summary>
        /// <param name="corsProperties">Cors Rules from XSCL</param>
        /// <returns>PSCorsRule Array</returns>
        public static PSCorsRule[] ParseCorsRules(CorsProperties corsProperties)
        {
            List <PSCorsRule> ruleList = new List <PSCorsRule>();

            if (corsProperties != null && corsProperties.CorsRules != null)
            {
                foreach (var corsRule in corsProperties.CorsRules)
                {
                    PSCorsRule psCorsRule = new PSCorsRule();
                    psCorsRule.AllowedOrigins  = ListToArray(corsRule.AllowedOrigins);
                    psCorsRule.AllowedHeaders  = ListToArray(corsRule.AllowedHeaders);
                    psCorsRule.ExposedHeaders  = ListToArray(corsRule.ExposedHeaders);
                    psCorsRule.AllowedMethods  = ConvertCorsHttpMethodToString(corsRule.AllowedMethods);
                    psCorsRule.MaxAgeInSeconds = corsRule.MaxAgeInSeconds;
                    ruleList.Add(psCorsRule);
                }
            }

            return(ruleList.ToArray());
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: micajah/AzureFileService
        static void Main(string[] args)
        {
            CloudStorageAccount storageAccount    = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["mafs:StorageConnectionString"]);
            CloudBlobClient     client            = storageAccount.CreateCloudBlobClient();
            ServiceProperties   serviceProperties = client.GetServiceProperties();
            CorsProperties      corsSettings      = serviceProperties.Cors;

            AddRule(corsSettings);
            //corsSettings.CorsRules.RemoveAt(0);

            //serviceProperties.DefaultServiceVersion = "2015-07-08";

            client.SetServiceProperties(serviceProperties);

            Console.WriteLine("DefaultServiceVersion        : " + serviceProperties.DefaultServiceVersion);

            DisplayCorsSettings(corsSettings);

            Console.ReadKey();
        }
コード例 #10
0
        public async Task <string> EnableCORS()
        {
            // Enable CORS
            CorsProperties corsProps = new CorsProperties();

            corsProps.CorsRules.Add(new CorsRule
            {
                AllowedHeaders = new List <string> {
                    "*"
                },
                AllowedMethods = CorsHttpMethods.Get,
                AllowedOrigins = new List <string> {
                    "*"
                },
                ExposedHeaders = new List <string> {
                    "*"
                },
                MaxAgeInSeconds = 200
            });

            ServiceProperties serviceProps = new ServiceProperties
            {
                Cors    = corsProps,
                Logging = new LoggingProperties
                {
                    Version = "1.0",
                },
                HourMetrics = new MetricsProperties
                {
                    Version = "1.0"
                },
                MinuteMetrics = new MetricsProperties
                {
                    Version = "1.0"
                },
            };
            await blobClient.SetServicePropertiesAsync(serviceProps);

            return("Successfully set CORS policy, allowing GET on all origins.  See https://msdn.microsoft.com/en-us/library/azure/dn535601.aspx for more.");
        }
コード例 #11
0
ファイル: StartupExtensions.cs プロジェクト: MatZil/Testing
        public static void AddCorsRuleForAzure(this IApplicationBuilder app)
        {
            var corsRule = new CorsRule()
            {
                AllowedHeaders = new List <string> {
                    "*"
                },
                AllowedMethods = CorsHttpMethods.Get,
                AllowedOrigins = new List <string> {
                    "https://localhost:5001"
                },
                MaxAgeInSeconds = 3600,
            };

            string connectionString               = AzureStorageConfiguration.GetConnectionString();
            CloudStorageAccount storageAccount    = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient     client            = storageAccount.CreateCloudBlobClient();
            ServiceProperties   serviceProperties = client.GetServiceProperties();
            CorsProperties      corsSettings      = serviceProperties.Cors;

            corsSettings.CorsRules.Add(corsRule);
            client.SetServiceProperties(serviceProperties);
        }
コード例 #12
0
        /// <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);
            }
        }
コード例 #13
0
        /// <summary>
        /// Create a set of CorsRule that containers different parameters combination
        /// </summary>
        static private CorsProperties PrepareCoresproperties()
        {
            CorsProperties coresproperties = new CorsProperties();

            coresproperties.CorsRules.Add(
                new CorsRule()
            {
                AllowedHeaders = new List <string>
                {
                    "x-ms-meta-data*",
                    "x -ms-meta-target*",
                    "x -ms-meta-abc"
                },
                AllowedMethods = CorsHttpMethods.Connect | CorsHttpMethods.Delete | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Merge,
                AllowedOrigins = new List <string>
                {
                    "http://www.contoso.com",
                    "http://www.fabrikam.com"
                },
                ExposedHeaders = new List <string>
                {
                    "x-ms-meta-*"
                },
                MaxAgeInSeconds = 100
            });
            coresproperties.CorsRules.Add(
                new CorsRule()
            {
                AllowedHeaders = new List <string>
                {
                    "x -ms-meta-12345675754564*"
                },
                AllowedMethods = CorsHttpMethods.None,
                AllowedOrigins = new List <string>
                {
                    "http://www.abc23.com",
                    "https://www.fabrikam.com/*"
                },
                ExposedHeaders = new List <string>
                {
                    "x-ms-meta-data*",
                    "x -ms-meta-target*",
                    "x -ms-meta-abc"
                },
                MaxAgeInSeconds = 2000
            });
            coresproperties.CorsRules.Add(
                new CorsRule()
            {
                AllowedHeaders = new List <string>
                {
                    "*"
                },
                AllowedMethods = CorsHttpMethods.Options | CorsHttpMethods.Post | CorsHttpMethods.Put | CorsHttpMethods.Trace,
                AllowedOrigins = new List <string>
                {
                    "*"
                },
                ExposedHeaders = new List <string>
                {
                    "*",
                },
                MaxAgeInSeconds = 0
            });
            return(coresproperties);
        }
コード例 #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FileServiceProperties"/> class.
 /// </summary>
 public FileServiceProperties(MetricsProperties hourMetrics = null, MetricsProperties minuteMetrics = null, CorsProperties cors = null)
 {
     this.serviceProperties = new ServiceProperties(null, hourMetrics, minuteMetrics, cors);
 }
コード例 #15
0
        static void Main(string[] args)
        {
            ConsoleColor original = Console.ForegroundColor;

            try
            {
                // Get Info From User
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("***************** Azure AD B2C Blob Storage Helper Tool *********************");
                Console.WriteLine("This tool will upload all contents of a directory to an Azure Blob Storage Container you specify.");
                Console.WriteLine("It will also enable CORS access from all origins on each of the files.");
                Console.WriteLine("");
                Console.WriteLine("Enter your Azure Storage Account name: ");
                string storageAccountName = Console.ReadLine();
                Console.WriteLine("Enter your Azure Blob Storage Primary Access Key: ");
                string storageKey = Console.ReadLine();
                Console.WriteLine("Enter your Azure Blob Storage Container name: ");
                string containerName = Console.ReadLine();
                Console.WriteLine("Enter the path to the directory whose contents you wish to upload: ");
                string directoryPath = Console.ReadLine();


                // Upload File to Blob Storage
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(String.Format(CultureInfo.InvariantCulture, connectionStringTemplate, storageAccountName, storageKey));
                CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer  container      = blobClient.GetContainerReference(containerName);

                string   absoluteDirPath = Path.GetFullPath(directoryPath);
                string[] allFiles        = Directory.GetFiles(absoluteDirPath, "*.*", SearchOption.AllDirectories);
                foreach (string filePath in allFiles)
                {
                    string relativePathAndFileName = filePath.Substring(filePath.IndexOf(absoluteDirPath) + absoluteDirPath.Length);
                    relativePathAndFileName = relativePathAndFileName[0] == '\\' ? relativePathAndFileName.Substring(1) : relativePathAndFileName;
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(relativePathAndFileName);
                    blockBlob.Properties.ContentType = MapFileExtensionToContentType(relativePathAndFileName);
                    using (var fileStream = System.IO.File.OpenRead(filePath))
                    {
                        blockBlob.UploadFromStream(fileStream);
                    }

                    Console.WriteLine("Sucessfully uploaded file " + relativePathAndFileName + " to Azure Blob Storage");
                }

                // Enable CORS
                CorsProperties corsProps = new CorsProperties();
                corsProps.CorsRules.Add(new CorsRule
                {
                    AllowedHeaders = new List <string> {
                        "*"
                    },
                    AllowedMethods = CorsHttpMethods.Get,
                    AllowedOrigins = new List <string> {
                        "*"
                    },
                    ExposedHeaders = new List <string> {
                        "*"
                    },
                    MaxAgeInSeconds = 200
                });

                ServiceProperties serviceProps = new ServiceProperties
                {
                    Cors    = corsProps,
                    Logging = new LoggingProperties
                    {
                        Version = "1.0",
                    },
                    HourMetrics = new MetricsProperties
                    {
                        Version = "1.0"
                    },
                    MinuteMetrics = new MetricsProperties
                    {
                        Version = "1.0"
                    },
                };
                blobClient.SetServiceProperties(serviceProps);

                Console.WriteLine("Successfully set CORS policy, allowing GET on all origins.  See https://msdn.microsoft.com/en-us/library/azure/dn535601.aspx for more.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error Making Request to Azure Blob Storage: ");
                Console.WriteLine("");
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("Press Enter to close...");
                Console.ReadLine();
                Console.ForegroundColor = original;
            }
        }
コード例 #16
0
 public FileServiceProperties(MetricsProperties hourMetrics = null, MetricsProperties minuteMetrics = null, CorsProperties cors = null)
 {
     throw new System.NotImplementedException();
 }