【发布时间】: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