I am getting system null exception and following code is as shown below;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Azure;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Auth;
using Microsoft.Azure.Storage.Blob.Protocol;
using Microsoft.Azure.Storage.Blob;
using System.Threading;
namespace EncryptionApplication
{
class Program
{
private async static Task<string> GetToken(string authority, string resource, string scope)
{
var authContext = new AuthenticationContext(authority);
ClientCredential clientCred = new ClientCredential(CloudConfigurationManager.GetSetting("clientId"), CloudConfigurationManager.GetSetting("clientSecret"));
AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);
if(result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
return result.AccessToken;
}
private static async Task ResolveKeyAsync()
{
KeyVaultKeyResolver cloudResolver = new KeyVaultKeyResolver(GetToken);
var rsa = cloudResolver.ResolveKeyAsync("https://entsavmvault.vault.azure.net/keys/MySecret10", CancellationToken.None).GetAwaiter().GetResult();
BlobEncryptionPolicy policy = new BlobEncryptionPolicy(rsa, null);
BlobRequestOptions options = new BlobRequestOptions() { EncryptionPolicy = policy };
//CloudBlobContainer blob = contain.GetBlockBlobReference("file.txt");
//using (var stream = System.IO.File.OpenRead(@"C:\Temp\File.txt")) blob.UploadFromStream(stream, stream.Length, null, options, null);
}
static void Main(string[] args)
{
StorageCredentials creds = new StorageCredentials(CloudConfigurationManager.GetSetting("accountName"), CloudConfigurationManager.GetSetting("accountKey"));
CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer contain = client.GetContainerReference(CloudConfigurationManager.GetSetting("container"));
contain.CreateIfNotExists();
KeyVaultKeyResolver cloudResolver = new KeyVaultKeyResolver(GetToken);
}
}
}
on my App.config file is my list of fields mapped;
<appSettings>
<add key="accountName" value="resource-campus"/>
<add key="accountKey" value="entsavmvault"/>
<add key="clientId" value="https://entsavmvault.vault.azure.net/secrets/AppSecret/*********"/>
<add key="clientSecret" value="MySecret10"/>
<add key="container" value="stuff"/>
</appSettings>
This code is throwing a system exception for value key being empty(accountName). What am i missing from this peace of code? Please assist me, thanks.