【问题标题】:Azure rsaKey from KeyVaultKeyResolver is always nullKeyVaultKeyResolver 中的 Azure rsaKey 始终为空
【发布时间】:2017-08-29 16:50:22
【问题描述】:

我正在通过我的 MVC/Durandal Web 应用程序将身份文件保存到 Azure blob 存储。我正在关注this 示例,以使用 Azure 密钥库加密 Azure 存储中的 blob 来存储加密机密。

这是我的代码:

公共异步任务 UploadIdentityDocumentForClient(string fileName, ParsedClientModel parsedClientModel) { BlobRequestOptions 选项 = 等待 GetBlobRequestOptions(); 等待 _storageRepository.CreateEncryptedBlobFromByteArray(_storageManager, _containerName, fileName, parsedClientModel.IdentityDocumentFile, parsedClientModel.IdentityDocumentContentType, options); 返回文件名; } 私有静态异步任务 GetBlobRequestOptions() { string secretUri = WebConfigurationManager.AppSettings["SecretUri"]; string secretName = WebConfigurationManager.AppSettings["SecretEncryptionName"]; *1 KeyVaultKeyResolver keyVaultKeyResolver = new KeyVaultKeyResolver(GetAccessToken); *2 IKey rsaKey = keyVaultKeyResolver.ResolveKeyAsync($"{secretUri}/secrets/{secretName}", CancellationToken.None).GetAwaiter().GetResult(); BlobEncryptionPolicy 策略 = 新 BlobEncryptionPolicy(rsaKey, null); BlobRequestOptions 选项 = 新的 BlobRequestOptions { EncryptionPolicy = 策略 }; 退货选项; } 公共静态异步任务GetAccessToken(字符串权限,字符串资源,字符串范围) { 字符串 clientId = WebConfigurationManager.AppSettings["ClientId"]; 字符串 clientSecret = WebConfigurationManager.AppSettings["ClientSecret"]; ClientCredential clientCredential = new ClientCredential(clientId, clientSecret); AuthenticationContext authenticationContext = new AuthenticationContext(authority, TokenCache.DefaultShared); AuthenticationResult 结果 = 等待 authenticationContext.AcquireTokenAsync(resource, clientCredential); 如果(结果 == 空) { 抛出新的 InvalidOperationException( “GetAccessToken - 无法获取应用程序的 Active Directory 令牌。”); } *3 返回结果.AccessToken; } 公共异步任务 CreateEncryptedBlobFromByteArray(IStorageManager storageManager, string containerName, string fileName, byte[] byteArray、字符串 contentType、BlobRequestOptions 选项) { CloudBlobContainer 容器 = 等待 CreateStorageContainerIfNotExists(storageManager, containerName); CloudBlockBlob blob = container.GetBlockBlobReference(fileName); blob.Properties.ContentType = contentType; 等待 blob.UploadFromByteArrayAsync(byteArray, 0, byteArray.Length, AccessCondition.GenerateEmptyCondition(), options, new OperationContext()); }

这一行...

IKey rsaKey = keyVaultKeyResolver.ResolveKeyAsync($"{secretUri}/secrets/{secretName}", CancellationToken.None).GetAwaiter().GetResult();

总是返回 null。

我在上面的代码中添加了断点(*1 到 *3),并且注意到 *2 总是在 *3 之前被命中。这意味着 KeyVaultKeyResolver(GetAccessToken) 调用不会等待 GetAccessToken 调用返回值。

关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: c# azure encryption storage azure-keyvault


    【解决方案1】:

    我发现我做错了什么。

    断点 2 所在的位置我应该使用此代码:

    SymmetricKey sec = (SymmetricKey) cloudResolver
                .ResolveKeyAsync("https://yourkeyvault.vault.azure.net/secrets/MiplanAdminLocalEncryption",
                    CancellationToken.None)
                .GetAwaiter()
                .GetResult();
    

    我还必须使用 PowerShell 将机密添加到我的 Azure Key Vault。通过管理 UI 创建密钥不起作用。以下是我使用的命令:

    抱歉图片,但即使粘贴为代码示例,SO 也不会接受上述文本。

    原始示例请参见this 站点。

    我找到了一种通过 Azure 门户添加机密的方法:

        //If entering via Azure UI:
        //Your secret string must be 16 characters (28 bits) long or end up being 28, 192, 256, 384, or 512 bits.
        // Base64 encode using https://www.base64encode.org/
        //Take this encoded value and enter it as the secret value in the UI.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 2012-03-10
      • 2011-10-13
      • 2021-09-01
      • 2021-05-05
      相关资源
      最近更新 更多