0

I need to get the keys through code, not through a portal. For doing this I have found REST API in Google. This is the link to Azure Key management API, but do this we need to do an authentication.

We have to develop all this using C# only.

karel
  • 3,880
  • 31
  • 37
  • 42
NarsingRao
  • 47
  • 6

1 Answers1

1

Regarding the issue, please refer to the following code.

 #install Microsoft.Azure.Management.ResourceManager.Fluent and Microsoft.Azure.Management.Fluent    
string clientId = "client id";
     string secret = "secret key";
     string tenant = "tenant id";
     var functionName ="functionName";
     var webFunctionAppName = "functionApp name";
     string resourceGroup = "resource group name";
     var credentials = new AzureCredentials(new ServicePrincipalLoginInformation { ClientId = clientId, ClientSecret = secret}, tenant, AzureEnvironment.AzureGlobalCloud);
     var azure = Azure
              .Configure()
              .Authenticate(credentials)
              .WithDefaultSubscription();

     var webFunctionApp = azure.AppServices.FunctionApps.GetByResourceGroup(resourceGroup, webFunctionAppName);
     var ftpUsername = webFunctionApp.GetPublishingProfile().FtpUsername;
     var username = ftpUsername.Split('\\').ToList()[1];
     var password = webFunctionApp.GetPublishingProfile().FtpPassword;
     var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
     var apiUrl = new Uri($"https://{webFunctionAppName}.scm.azurewebsites.net/api");
     var siteUrl = new Uri($"https://{webFunctionAppName}.azurewebsites.net");
     string JWT;
     using (var client = new HttpClient())
      {
         client.DefaultRequestHeaders.Add("Authorization", $"Basic {base64Auth}");

         var result = client.GetAsync($"{apiUrl}/functions/admin/token").Result;
         JWT = result.Content.ReadAsStringAsync().Result.Trim('"'); //get  JWT for call funtion key
       }
     using (var client = new HttpClient())
     {
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + JWT);
        var key = client.GetAsync($"{siteUrl}/admin/functions/{functionName}/keys").Result.Content.ReadAsStringAsync().Result;
      }

Besides, you also can refer to the document.

Jim Xu
  • 15,664
  • 2
  • 7
  • 21
  • The client '9671c613-52b4-42d4-b260-6fa0c7ab6bff' with object id '9671c613-52b4-42d4-b260-6fa0c7ab6bff' does not have authorization to perform action 'Microsoft.Web/sites/read' over scope '/subscriptions/1ab4f7c1-3d23-419b-9d6c-cae2423ca16d/resourceGroups/conceptone/providers/Microsoft.Web/sites/imsfap'. – NarsingRao Feb 26 '19 at 10:12
  • Could anyone help me on this? – NarsingRao Feb 26 '19 at 10:13
  • string clientId = "8e636aed-a0c6-47a1-af77-137f96e262b8"; string secret = "g5sL6PDyNY4a65Ely7pXXpO/UqUwVvIKBEaTTdEuRY4="; string tenant = "2d4d44fe-dba2-49bc-8447-2f0602a1bfe1"; var functionName = "Ims"; var webFunctionAppName = "imsfap"; string resourceGroup = "conceptone"; string SubscriptionId = "1ab4f7c1-3d23-419b-9d6c-cae2423ca16d"; these are parameters i have passed – NarsingRao Feb 26 '19 at 10:14
  • The same code what u have provided the same one i have used – NarsingRao Feb 26 '19 at 10:28
  • Have you assigned a role to your account? For more details, please refer to https://docs.microsoft.com/en-us/azure/role-based-access-control/rbac-and-directory-admin-roles#azure-rbac-roles. – Jim Xu Feb 27 '19 at 01:21