【问题标题】:System.argumentnullexception is null using microsoft azure storageSystem.argumentnullexception 为 null 使用 Microsoft azure 存储
【发布时间】:2019-08-26 13:01:09
【问题描述】:

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

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>

此代码因值键为空(帐户名)而引发系统异常。我从这种和平的代码中缺少什么?请帮助我,谢谢。

【问题讨论】:

  • 异常来自哪两个异步函数?请在您的代码中找到该行并用注释标记它。
  • StorageCredentials creds = new StorageCredentials(CloudConfigurationManager.GetSetting("accountName"), CloudConfigurationManager.GetSetting("accountKey"));
  • 即一行3个操作。我最好的建议是将其分成 3 行,在传递之前使用临时变量来存储每个参数。这样你就可以检查哪一个是罪魁祸首。它们是基于字符串索引/键的查找。所以他们返回null的机会很高。 |我还觉得我应该指出,使用静态类提供设置是一个坏主意。静态设计是全球性的,而全球性的东西到处都是个坏主意。
  • 请尝试使用“System.Configuration.ConfigurationManager.AppSettings[key];”从您的 .config 文件中获取 appsettings
  • 在主函数的第一行,尝试 Console.WriteLine(CloudConfigurationManager.GetSetting("accountName"))。您能否向我们证明您正在正确检索配置设置?

标签: c# encryption azure-keyvault


【解决方案1】:

如果您想在 C# 控制台应用程序中从 App.config 文件中获取 appsettings 值,您可以使用System.Configuration.ConfigurationManager.AppSettings.get("the name of the 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();

更多详情请参考article

【讨论】:

    猜你喜欢
    • 2020-11-16
    • 2015-05-09
    • 2023-03-25
    • 2018-12-16
    • 2018-12-17
    • 2011-03-26
    • 2016-08-08
    • 2014-08-04
    • 2019-10-13
    相关资源
    最近更新 更多