Impossibile accedere ad Azure Key Vault dall'app console desktop

Impossibile accedere ad Azure Key Vault dall'app console desktop

Il blog di Mark è stato estremamente utile, da quel blog ho imparato come farlo e di seguito sono riportati i passaggi e il codice a partire dal 6 novembre 2018.

Riepilogo dei passaggi:

  1. Registra app
  2. Crea chiave all'interno di questa app appena registrata
  3. Crea Key Vault e assegna l'autorizzazione all'app
  4. Crea segreto all'interno del caveau

Accedi tramite codice

using Microsoft.Azure.KeyVault;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Experiments.AzureKeyValut
{
    internal class AzureKeyValueDemo
    {
        private static async Task Main(string[] args)
        {
            await GetSecretAsync("https://YOURVAULTNAME.vault.azure.net/", "YourSecretKey");
        }

        private static async Task<string> GetSecretAsync(string vaultUrl, string vaultKey)
        {
            var client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessTokenAsync), new HttpClient());
            var secret = await client.GetSecretAsync(vaultUrl, vaultKey);

            return secret.Value;
        }

        private static async Task<string> GetAccessTokenAsync(string authority, string resource, string scope)
        {
            //DEMO ONLY
            //Storing ApplicationId and Key in code is bad idea :)
            var appCredentials = new ClientCredential("YourApplicationId", "YourApplicationKey");
            var context = new AuthenticationContext(authority, TokenCache.DefaultShared);

            var result = await context.AcquireTokenAsync(resource, appCredentials);

            return result.AccessToken;
        }
    }
}

Come registrare la tua app:

Come creare la password dell'app di Azure e ottenere l'ID dell'app

Come creare Azure Key Vault e assegnare autorizzazioni

Come creare segreti di Azure

Come accedervi tramite codice


Oltre a ciò che Tom ha fornito, dopo aver finalmente capito come far funzionare le cose, ho documentato ciò che ho imparato su https://jumpforjoysoftware.com/2017/12/azure-key-vaults/. Si spera che tutto questo risparmierà alle persone una seria frustrazione.


Dopo aver registrato l'app Directory di Azure, è necessario assegnare il ruolo all'applicazione. se vogliamo utilizzare Azure Key Vault, dobbiamo anche concedere l'autorizzazione per utilizzare Key Vault. La risorsa per Key Vault è https://vault.azure.net . Potresti anche ottenere informazioni più dettagliate da un altro thread SO.

Codice demo:

 static string appId = "application Id";
 static string tenantId = "tenant id";
 static string uri = "http://localhost:13526"; //redirect uri
 static void Main(string[] args)
 {
    var kv = new KeyVaultClient(GetAccessToken);
    var scret = kv.GetSecretAsync("https://xxxx.vault.azure.net", "xxxx").GetAwaiter().GetResult();
 }

 public static async Task<string> GetAccessToken(string azureTenantId,string clientId,string redirectUri)
 {
       var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
       var tokenResult = await context.AcquireTokenAsync("https://vault.azure.net", appId, new Uri(uri), new PlatformParameters(PromptBehavior.SelectAccount));
       return tokenResult.AccessToken;
  }