public async Task SharedKeyAuthAsync()
        {
            // Get a Storage account name, shared key, and endpoint Uri.
            //
            // You can obtain both from the Azure Portal by clicking Access
            // Keys under Settings in the Portal Storage account blade.
            //
            // You can also get access to your account keys from the Azure CLI
            // with:
            //
            //     az storage account keys list --account-name <account_name> --resource-group <resource_group>
            //
            string accountName = StorageAccountName;
            string accountKey  = StorageAccountKey;
            Uri    serviceUri  = StorageAccountFileUri;

            // Create a SharedKeyCredential that we can use to authenticate
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);

            // Create a client that can authenticate with a connection string
            ShareServiceClient service = new ShareServiceClient(serviceUri, credential);

            // Make a service request to verify we've successfully authenticated
            await service.GetPropertiesAsync();
        }
        /// <summary>
        /// Query the Cross-Origin Resource Sharing (CORS) rules for the File service
        /// </summary>
        /// <param name="shareServiceClient"></param>
        private static async Task CorsSample(ShareServiceClient shareServiceClient)
        {
            Console.WriteLine();

            // Get service properties
            Console.WriteLine("Get service properties");
            ShareServiceProperties originalProperties = await shareServiceClient.GetPropertiesAsync();

            try
            {
                // Add CORS rule
                Console.WriteLine("Add CORS rule");

                var corsRule = new ShareCorsRule
                {
                    AllowedHeaders  = "*",
                    AllowedMethods  = "GET",
                    AllowedOrigins  = "*",
                    ExposedHeaders  = "*",
                    MaxAgeInSeconds = 3600
                };

                ShareServiceProperties serviceProperties = await shareServiceClient.GetPropertiesAsync();

                serviceProperties.Cors.Clear();
                serviceProperties.Cors.Add(corsRule);

                await shareServiceClient.SetPropertiesAsync(serviceProperties);

                Console.WriteLine("Set property successfully");
            }
            finally
            {
                // Revert back to original service properties
                Console.WriteLine("Revert back to original service properties");
                await shareServiceClient.SetPropertiesAsync(originalProperties);

                Console.WriteLine("Revert properties successfully.");
            }

            Console.WriteLine();
        }
Ejemplo n.º 3
0
        // </snippet_DeleteSnapshot>

        // <snippet_UseMetrics>
        //-------------------------------------------------
        // Use metrics
        //-------------------------------------------------
        public async Task UseMetricsAsync()
        {
            // Get the connection string from app settings
            string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

            // Instatiate a ShareServiceClient
            ShareServiceClient shareService = new ShareServiceClient(connectionString);

            // Set metrics properties for File service
            await shareService.SetPropertiesAsync(new ShareServiceProperties()
            {
                // Set hour metrics
                HourMetrics = new ShareMetrics()
                {
                    Enabled     = true,
                    IncludeApis = true,
                    Version     = "1.0",

                    RetentionPolicy = new ShareRetentionPolicy()
                    {
                        Enabled = true,
                        Days    = 14
                    }
                },

                // Set minute metrics
                MinuteMetrics = new ShareMetrics()
                {
                    Enabled     = true,
                    IncludeApis = true,
                    Version     = "1.0",

                    RetentionPolicy = new ShareRetentionPolicy()
                    {
                        Enabled = true,
                        Days    = 7
                    }
                }
            });

            // Read the metrics properties we just set
            ShareServiceProperties serviceProperties = await shareService.GetPropertiesAsync();

            // Display the properties
            Console.WriteLine();
            Console.WriteLine($"HourMetrics.InludeApis: {serviceProperties.HourMetrics.IncludeApis}");
            Console.WriteLine($"HourMetrics.RetentionPolicy.Days: {serviceProperties.HourMetrics.RetentionPolicy.Days}");
            Console.WriteLine($"HourMetrics.Version: {serviceProperties.HourMetrics.Version}");
            Console.WriteLine();
            Console.WriteLine($"MinuteMetrics.InludeApis: {serviceProperties.MinuteMetrics.IncludeApis}");
            Console.WriteLine($"MinuteMetrics.RetentionPolicy.Days: {serviceProperties.MinuteMetrics.RetentionPolicy.Days}");
            Console.WriteLine($"MinuteMetrics.Version: {serviceProperties.MinuteMetrics.Version}");
            Console.WriteLine();
        }
        public async Task ConnectionStringAsync()
        {
            // Get a connection string to our Azure Storage account.  You can
            // obtain your connection string from the Azure Portal (click
            // Access Keys under Settings in the Portal Storage account blade)
            // or using the Azure CLI with:
            //
            //     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
            //
            // And you can provide the connection string to your application
            // using an environment variable.
            string connectionString = ConnectionString;

            // Create a client that can authenticate with a connection string
            ShareServiceClient service = new ShareServiceClient(connectionString);

            // Make a service request to verify we've successfully authenticated
            await service.GetPropertiesAsync();
        }
        public async Task SharedAccessSignatureAuthAsync()
        {
            // Create a service level SAS that only allows reading from service
            // level APIs
            AccountSasBuilder sas = new AccountSasBuilder
            {
                // Allow access to files
                Services = AccountSasServices.Files,

                // Allow access to the service level APIs
                ResourceTypes = AccountSasResourceTypes.Service,

                // Access expires in 1 hour!
                ExpiresOn = DateTimeOffset.UtcNow.AddHours(1)
            };

            // Allow read access
            sas.SetPermissions(AccountSasPermissions.Read);

            // Create a SharedKeyCredential that we can use to sign the SAS token
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(StorageAccountName, StorageAccountKey);

            // Build a SAS URI
            UriBuilder sasUri = new UriBuilder(StorageAccountFileUri);

            sasUri.Query = sas.ToSasQueryParameters(credential).ToString();

            // Create a client that can authenticate with the SAS URI
            ShareServiceClient service = new ShareServiceClient(sasUri.Uri);

            // Make a service request to verify we've successfully authenticated
            await service.GetPropertiesAsync();

            // Try to create a new container (which is beyond our
            // delegated permission)
            RequestFailedException ex =
                Assert.ThrowsAsync <RequestFailedException>(
                    async() => await service.CreateShareAsync(Randomize("sample-share")));

            Assert.AreEqual(403, ex.Status);
        }