C# 使用microsoft azure存储时System.argumentnullexception为null

C# 使用microsoft azure存储时System.argumentnullexception为null,c#,encryption,azure-keyvault,C#,Encryption,Azure Keyvault,我得到系统空异常,下面的代码如下所示 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

我得到系统空异常,下面的代码如下所示

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);
        }
    }
}
在我的App.config文件中是我映射的字段列表

<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>

此代码正在为值键emptyaccountName引发系统异常。我在这和平的代码中错过了什么?请帮助我,谢谢

如果要在C控制台应用程序中从App.config文件中获取appsettings值,可以使用System.Configuration.ConfigurationManager.appsettings.getthe key的名称来获取它们。例如:

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();

有关更多详细信息,请参阅

如果要从C控制台应用程序中的App.config文件获取appsettings值,可以使用System.Configuration.ConfigurationManager.appsettings.getthe键的名称来获取它们。例如:

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();

有关更多详细信息,请参阅

在2个异步函数的混乱中,异常来自何处?请找到该行并在代码中标记注释。StorageCredentials creds=new StorageCredentials CloudConfigurationManager.GetSettingaccountName,CloudConfigurationManager.GetSettingaccountKey;也就是说,一条生产线中有3个操作。我最好的建议是将它分成3行,在传递之前使用临时变量存储每个参数。这样你就可以检查哪一个是最棒的。它们是基于字符串索引/键的查找。因此,它们返回空值的可能性很高我还觉得我应该指出,使用静态类提供设置是一个坏主意。静态是按设计全局的,全局内容是一个坏主意。请尝试使用“System.Configuration.ConfigurationManager.AppSettings[key];”从主函数第一行的.config文件中获取AppSettings,请尝试Console.WriteLineCloudConfiguration Manager.GetSettingaccountName。您能向我们证明您正确地检索了配置设置吗?在这混乱的2个异步函数中,异常从何而来?请找到该行并在代码中标记注释。StorageCredentials creds=new StorageCredentials CloudConfigurationManager.GetSettingaccountName,CloudConfigurationManager.GetSettingaccountKey;也就是说,一条生产线中有3个操作。我最好的建议是将它分成3行,在传递之前使用临时变量存储每个参数。这样你就可以检查哪一个是最棒的。它们是基于字符串索引/键的查找。因此,它们返回空值的可能性很高我还觉得我应该指出,使用静态类提供设置是一个坏主意。静态是按设计全局的,全局内容是一个坏主意。请尝试使用“System.Configuration.ConfigurationManager.AppSettings[key];”从主函数第一行的.config文件中获取AppSettings,请尝试Console.WriteLineCloudConfiguration Manager.GetSettingaccountName。您能否向我们证明您正在正确检索配置设置?