2

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.

Gcobza
  • 432
  • 1
  • 5
  • 12
  • 1
    Where in that mess of 2 async functions does the exception come from? Please find the line and mark it with a comment in your code. – Christopher Aug 26 '19 at 13:02
  • StorageCredentials creds = new StorageCredentials(CloudConfigurationManager.GetSetting("accountName"), CloudConfigurationManager.GetSetting("accountKey")); – Gcobza Aug 26 '19 at 14:10
  • That is 3 operations in one line. My best advise is to split that up into 3 lines, using temporary variables to store each argument before handing it off. That way you can check wich one is the cultript. They are lookups based on a string index/key. So chances thare high that they will return null. | I also feel that I should point out that using a static class for settings providing is a bad idea. Statics are by design global and global stuff is a bad idea all around. – Christopher Aug 26 '19 at 14:23
  • Please try to use “System.Configuration.ConfigurationManager.AppSettings[key];” to get appsettings from your .config file –  Aug 26 '19 at 14:27
  • In the first line of your main function, try Console.WriteLine(CloudConfigurationManager.GetSetting("accountName")). Can you prove to us that you're retrieving the configuration setting correctly? – chill94 Aug 26 '19 at 16:34
  • Let me try and i will revert shortly – Gcobza Aug 27 '19 at 08:56

1 Answers1

-1

If you want to get the appsettings value form App.config file in C# Console application, you can use System.Configuration.ConfigurationManager.AppSettings.get("the name of the key") to get them. For example:

var accountName = ConfigurationManager.AppSettings.Get("accountName");
var accountKey = ConfigurationManager.AppSettings.Get("accountKey");
var container = ConfigurationManager.AppSettings.Get("container");
StorageCredentials creds = new StorageCredentials(accountName,accountKey);
CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer contain = client.GetContainerReference(container);
contain.CreateIfNotExists();
Console.WriteLine(contain.Uri);
Console.ReadLine();

enter image description here

For more details, please refer to the article

Jim Xu
  • 21,610
  • 2
  • 19
  • 39