private async Task <CloudStorageAccount> GetStorageCredential()
        {
            AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider((string)null, "https://login.microsoftonline.com/");
            NewTokenAndFrequency      tokenAndFrequency1        = await TokenRenewerAsync((object)azureServiceTokenProvider, CancellationToken.None);

            NewTokenAndFrequency tokenAndFrequency2 = tokenAndFrequency1;

            tokenAndFrequency1 = new NewTokenAndFrequency();
            TokenCredential     tokenCredential     = new TokenCredential(tokenAndFrequency2.Token, new RenewTokenFuncAsync(TokenRenewerAsync), (object)azureServiceTokenProvider, tokenAndFrequency2.Frequency.Value);
            CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(new StorageCredentials(tokenCredential), AccountName, "core.windows.net", true);

            return(cloudStorageAccount);
        }
Beispiel #2
0
        public async Task <string> Get()
        {
            try
            {
                // source https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad-msi

                string storageBaseUri = $"https://{MyStorageAccountName}.blob.core.windows.net";

                // Get the initial access token and the interval at which to refresh it.
                AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
                NewTokenAndFrequency      tokenAndFrequency         = await TokenRenewerAsync(azureServiceTokenProvider, CancellationToken.None);

                // Create storage credentials using the initial token, and connect the callback function
                // to renew the token just before it expires
                TokenCredential tokenCredential = new TokenCredential(tokenAndFrequency.Token, TokenRenewerAsync, azureServiceTokenProvider, tokenAndFrequency.Frequency.Value);

                StorageCredentials storageCredentials = new StorageCredentials(tokenCredential);

                CloudBlobClient    client    = new CloudBlobClient(new Uri(storageBaseUri), storageCredentials);
                CloudBlobContainer container = client.GetContainerReference(SampleContainer);

                await container.CreateIfNotExistsAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference(Blob1Txt);

                if (!blob.Exists())
                {
                    // Upload text to the blob.
                    await blob.UploadTextAsync($"That's ma blob! '{blob.Name}'");
                }


                return(await blob.DownloadTextAsync());
            }
            catch (Exception e)
            {
                // only for meetup purposes only, don't do this in production since it is a security risk! (information leakage)
                return(e.ToString());
            }
        }