2017-04-04 5 views
0

私のMVC/Durandal Webアプリケーションを使用してアイデンティティ文書をAzureブロブストレージに保存しています。私はthisの例に従うと、Azure鍵保管庫を使用してAzureストレージ内のブロブを暗号化し、暗号化秘密を保管しています。KeyVaultKeyResolverのAzure rsaKeyは常にnullです。

 

    public async Task UploadIdentityDocumentForClient(string fileName, ParsedClientModel parsedClientModel) 
    { 
     BlobRequestOptions options = await GetBlobRequestOptions(); 
     await 
      _storageRepository.CreateEncryptedBlobFromByteArray(_storageManager, _containerName, fileName, parsedClientModel.IdentityDocumentFile, parsedClientModel.IdentityDocumentContentType, options); 
     return fileName; 
    } 


    private static async Task 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 policy = new BlobEncryptionPolicy(rsaKey, null); 
     BlobRequestOptions options = new BlobRequestOptions 
     { 
      EncryptionPolicy = policy 
     }; 
     return options; 
    } 


    public static async Task GetAccessToken(string authority, string resource, string scope) 
    { 
     string clientId = WebConfigurationManager.AppSettings["ClientId"]; 
     string clientSecret = WebConfigurationManager.AppSettings["ClientSecret"]; 
     ClientCredential clientCredential = new ClientCredential(clientId, clientSecret); 
     AuthenticationContext authenticationContext = new AuthenticationContext(authority, TokenCache.DefaultShared); 
     AuthenticationResult result = await authenticationContext.AcquireTokenAsync(resource, clientCredential); 
     if (result == null) 
     { 
      throw new InvalidOperationException(
       "GetAccessToken - Failed to obtain the Active Directory token for application."); 
     } 
    *3 return result.AccessToken; 
    } 


    public async Task CreateEncryptedBlobFromByteArray(IStorageManager storageManager, string containerName, string fileName, 
     byte[] byteArray, string contentType, BlobRequestOptions options) 
    { 
     CloudBlobContainer container = await CreateStorageContainerIfNotExists(storageManager, containerName); 
     CloudBlockBlob blob = container.GetBlockBlobReference(fileName); 
     blob.Properties.ContentType = contentType; 
     await 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呼び出しが値を返してくるのを待っていないことを意味します。

私が間違っていることについてのアイデアはありますか?

答えて

0

私が間違っていたことを理解しました。

ブレークポイント2が、私はこのコードを使用している必要がありますされ

は:

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

私はまた、PowerShellを使用して、私のアズールキーVaultに秘密を追加する必要がありました。管理UIを介して秘密情報を作成することはできませんでした。画像のため申し訳ありませんが、SO

enter image description here

のサンプルコードとして貼り付けた場合でも、上記のテキストを受け入れないだろう。ここで私が使用するコマンドです。

元の例については、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. 
関連する問題