Esempio n. 1
0
        /// <summary>
        /// Generates XML representing the given CORS properties.
        /// </summary>
        /// <param name="cors">The CORS properties.</param>
        /// <returns>An XML logging element.</returns>
        private static XElement GenerateCorsXml(CorsProperties cors)
        {
            CommonUtility.AssertNotNull("cors", cors);

            IList <CorsRule> corsRules = cors.CorsRules;

            XElement ret = new XElement(CorsName);

            foreach (CorsRule rule in corsRules)
            {
                if (rule.AllowedOrigins.Count < 1 || rule.AllowedMethods == CorsHttpMethods.None || rule.MaxAgeInSeconds < 0)
                {
                    throw new InvalidOperationException(SR.InvalidCorsRule);
                }

                XElement ruleElement = new XElement(
                    CorsRuleName,
                    new XElement(AllowedOriginsName, string.Join(",", rule.AllowedOrigins.ToArray())),
                    new XElement(AllowedMethodsName, rule.AllowedMethods.ToString().Replace(" ", string.Empty).ToUpperInvariant()),
                    new XElement(ExposedHeadersName, string.Join(",", rule.ExposedHeaders.ToArray())),
                    new XElement(AllowedHeadersName, string.Join(",", rule.AllowedHeaders.ToArray())),
                    new XElement(MaxAgeInSecondsName, rule.MaxAgeInSeconds));

                ret.Add(ruleElement);
            }

            return(ret);
        }
Esempio n. 2
0
        /// <summary>
        /// Constructs a <c>CorsProperties</c> object from an XML element.
        /// </summary>
        /// <param name="element">The XML element.</param>
        /// <returns>A <c>CorsProperties</c> object containing the properties in the element.</returns>
        private static CorsProperties ReadCorsPropertiesFromXml(XElement element)
        {
            CorsProperties ret = new CorsProperties();

            // Check that CORS properties exist
            if (element == null)
            {
                return(ret);
            }

            IEnumerable <XElement> corsRules = element.Descendants(CorsRuleName);

            ret.CorsRules =
                corsRules.Select(
                    rule =>
                    new CorsRule
            {
                AllowedOrigins = rule.Element(AllowedOriginsName).Value.Split(',').ToList(),
                AllowedMethods =
                    (CorsHttpMethods)
                    Enum.Parse(
                        typeof(CorsHttpMethods), rule.Element(AllowedMethodsName).Value, true),
                AllowedHeaders =
                    rule.Element(AllowedHeadersName)
                    .Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .ToList(),
                ExposedHeaders =
                    rule.Element(ExposedHeadersName)
                    .Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .ToList(),
                MaxAgeInSeconds =
                    int.Parse(
                        rule.Element(MaxAgeInSecondsName).Value, CultureInfo.InvariantCulture)
            }).ToList();

            return(ret);
        }
        /// <summary>
        /// Constructs a <c>CorsProperties</c> object from an XML element.
        /// </summary>
        /// <param name="element">The XML element.</param>
        /// <returns>A <c>CorsProperties</c> object containing the properties in the element.</returns>
        private static CorsProperties ReadCorsPropertiesFromXml(XElement element)
        {
            CorsProperties ret = new CorsProperties();

            // Check that CORS properties exist
            if (element == null)
            {
                return ret;
            }

            IEnumerable<XElement> corsRules = element.Descendants(CorsRuleName);

            ret.CorsRules =
                corsRules.Select(
                    rule =>
                    new CorsRule
                    {
                        AllowedOrigins = rule.Element(AllowedOriginsName).Value.Split(',').ToList(),
                        AllowedMethods =
                            (CorsHttpMethods)
                            Enum.Parse(
                                typeof(CorsHttpMethods), rule.Element(AllowedMethodsName).Value, true),
                        AllowedHeaders =
                            rule.Element(AllowedHeadersName)
                                .Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                .ToList(),
                        ExposedHeaders =
                            rule.Element(ExposedHeadersName)
                                .Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                .ToList(),
                        MaxAgeInSeconds =
                            int.Parse(
                                rule.Element(MaxAgeInSecondsName).Value, CultureInfo.InvariantCulture)
                    }).ToList();

            return ret;
        }
        /// <summary>
        /// Generates XML representing the given CORS properties.
        /// </summary>
        /// <param name="cors">The CORS properties.</param>
        /// <returns>An XML logging element.</returns>
        private static XElement GenerateCorsXml(CorsProperties cors)
        {
            CommonUtility.AssertNotNull("cors", cors);

            IList<CorsRule> corsRules = cors.CorsRules;

            XElement ret = new XElement(CorsName);

            foreach (CorsRule rule in corsRules)
            {
                if (rule.AllowedOrigins.Count < 1 || rule.AllowedMethods == CorsHttpMethods.None || rule.MaxAgeInSeconds < 0)
                {
                    throw new InvalidOperationException(SR.InvalidCorsRule);
                }

                XElement ruleElement = new XElement(
                    CorsRuleName,
                    new XElement(AllowedOriginsName, string.Join(",", rule.AllowedOrigins.ToArray())),
                    new XElement(AllowedMethodsName, rule.AllowedMethods.ToString().Replace(" ", string.Empty).ToUpperInvariant()),
                    new XElement(ExposedHeadersName, string.Join(",", rule.ExposedHeaders.ToArray())),
                    new XElement(AllowedHeadersName, string.Join(",", rule.AllowedHeaders.ToArray())),
                    new XElement(MaxAgeInSecondsName, rule.MaxAgeInSeconds));

                ret.Add(ruleElement);
            }

            return ret;
        }
 /// <summary>
 /// Initializes a new instance of the ServiceProperties class.
 /// </summary>
 public ServiceProperties(LoggingProperties logging = null, MetricsProperties hourMetrics = null, MetricsProperties minuteMetrics = null, CorsProperties cors = null)
 {
     this.Logging = logging;
     this.HourMetrics = hourMetrics;
     this.MinuteMetrics = minuteMetrics;
     this.Cors = cors;
 }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of the ServiceProperties class.
 /// </summary>
 public ServiceProperties(LoggingProperties logging = null, MetricsProperties hourMetrics = null, MetricsProperties minuteMetrics = null, CorsProperties cors = null)
 {
     this.Logging       = logging;
     this.HourMetrics   = hourMetrics;
     this.MinuteMetrics = minuteMetrics;
     this.Cors          = cors;
 }
        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;
        }
Esempio n. 8
0
		/// <summary>
		/// Ensure javascript CORS requests from web APIs work.
		/// </summary>
		/// <remarks>See: http://msdn.microsoft.com/en-us/library/windowsazure/dn535601.aspx </remarks>
		/// <param name="client"></param>
		private static void ConfigureCorsAndLoggingOnStorageAccount(CloudBlobClient client)
		{
			CorsProperties cps = new CorsProperties();
			cps.CorsRules.Add(new CorsRule()
			{
				AllowedOrigins = new List<string>() { "*" },
				AllowedMethods = CorsHttpMethods.Get
			});
			ServiceProperties sp = new Microsoft.WindowsAzure.Storage.Shared.Protocol.ServiceProperties();
			sp.Cors = cps;
			sp.Logging.LoggingOperations = LoggingOperations.Read;
			sp.Logging.RetentionDays = 7;
			sp.Logging.Version = "1.0";
			sp.MinuteMetrics.MetricsLevel = MetricsLevel.ServiceAndApi;
			sp.MinuteMetrics.RetentionDays = 7;
			sp.MinuteMetrics.Version = "1.0";
			sp.HourMetrics.MetricsLevel = MetricsLevel.None;
			sp.HourMetrics.RetentionDays = 1;
			sp.HourMetrics.Version = "1.0";
			client.SetServiceProperties(sp,
			new BlobRequestOptions() { MaximumExecutionTime = TimeSpan.FromSeconds(300) }
			,
			null);
		}
 private static XElement GenerateCorsXml(CorsProperties cors)
 {
     throw new System.NotImplementedException();
 }
 public ServiceProperties(LoggingProperties logging = null, MetricsProperties hourMetrics = null, MetricsProperties minuteMetrics = null, CorsProperties cors = null)
 {
     throw new System.NotImplementedException();
 }
 /// <summary>
 /// Initializes a new instance of the ServiceProperties class.
 /// </summary>
 public ServiceProperties(LoggingProperties logging, MetricsProperties hourMetrics, MetricsProperties minuteMetrics, CorsProperties cors, DeleteRetentionPolicy deleteRetentionPolicy, StaticWebsiteProperties staticWebsite = null)
 {
     this.Logging               = logging;
     this.HourMetrics           = hourMetrics;
     this.MinuteMetrics         = minuteMetrics;
     this.Cors                  = cors;
     this.DeleteRetentionPolicy = deleteRetentionPolicy;
     this.StaticWebsite         = staticWebsite;
 }
 /// <summary>
 /// Initializes a new instance of the ServiceProperties class.
 /// </summary>
 public ServiceProperties(LoggingProperties logging = null, MetricsProperties hourMetrics = null, MetricsProperties minuteMetrics = null, CorsProperties cors = null, DeleteRetentionPolicy deleteRetentionPolicy = null)
     : this(logging, hourMetrics, minuteMetrics, cors, deleteRetentionPolicy, staticWebsite : null)
 {
 }
 /// <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);
 }
        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;
            }
        }
 /// <summary>
 /// Initializes a new instance of the ServiceProperties class.
 /// </summary>
 public ServiceProperties(LoggingProperties logging = null, MetricsProperties hourMetrics = null, MetricsProperties minuteMetrics = null, CorsProperties cors = null, DeleteRetentionPolicy deleteRetentionPolicy = null)
 {
     this.Logging               = logging;
     this.HourMetrics           = hourMetrics;
     this.MinuteMetrics         = minuteMetrics;
     this.Cors                  = cors;
     this.DeleteRetentionPolicy = deleteRetentionPolicy;
 }