Can't Access Azure Key Vault from desktop console app

前端 未结 4 1043
情话喂你
情话喂你 2020-12-30 06:55

I am having trouble accessing a secret from an Azure key vault. I suspect the problem is that I don\'t adequately understand the terminology, so the arguments I\'m supplying

4条回答
  •  轮回少年
    2020-12-30 07:18

    Help, or a reference to a really good example accessing key vaults from a console desktop app would be appreciated.

    After we registry the Azure Directory App then we need to assign role to application. if we want to operate Azure Key Vault, we also need to give permission to operate Key Vault. The resource for key vault is https://vault.azure.net. You also could get more detail info from another SO thread.

    Demo code:

     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 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;
      }
    

提交回复
热议问题