I almost went through all the SO answers related to this and other forums but I am still not able to figure out the actual issue. This app is console app which will be deployed on On-Premise environment and will upload files to Azure blob.
I am using WindowsAzure.Storage 9.3.3 with code below.
static void CreateBlob(string accountName, string containerName, string blobName, string accessToken)
{
var tokenCredential = new TokenCredential(accessToken);
var storageCredentials = new StorageCredentials(tokenCredential);
var storageAccount = new CloudStorageAccount(storageCredentials, accountName, string.Empty, useHttps: true);
var cloudBlobClient = storageAccount.CreateCloudBlobClient();
var cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(blobName);
string blobContents = "Blob created by Azure AD authenticated user.";
byte[] byteArray = Encoding.ASCII.GetBytes(blobContents);
try
{
using (MemoryStream stream = new MemoryStream(byteArray))
{
blockBlob.UploadFromStreamAsync(stream).Wait();
}
Console.WriteLine("upload successful");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
And to get the token like below.
public static string GetOAuthToken(string activeDirectoryTenantId, string
activeDirectoryApplicationSecret, string activeDirectoryApplicationId)
{
string resourceId = $"https://storage.azure.com";
var authority = String.Format("https://login.windows.net/" + activeDirectoryTenantId);
var credential = new ClientCredential(activeDirectoryApplicationId, activeDirectoryApplicationSecret);
var context = new AuthenticationContext(authority);
var result = context.AcquireTokenAsync(resourceId, credential).Result;
return result.AccessToken;
}
Could any one please suggest what I might be missing?
P/S I have already confirmed my local system is in sync with correct UTC time.


