Getting Unathorized when trying to get a secret from Azure key Vault

后端 未结 1 1478
终归单人心
终归单人心 2020-12-21 16:53

I\'m using Microsoft.Azure.keyVault trying to get a secret from a key vault in Azure.

I\'ve registered an application as both Native and Web API.

1条回答
  •  囚心锁ツ
    2020-12-21 17:28

    I test it with the following code, it works correctly on my side. The resourceUri is https://vault.azure.net

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

    The following is my detail steps.

    1.Registry an native application

    2.Add KeyVault Permission

    3.Add the above code and test it on my side.

    Packages.config

    
    
      
      
      
      
      
      
      
      
      
      
    
    

    0 讨论(0)
提交回复
热议问题