2

I'm developing an application that needs to run an AzureWebJob to deploy new instances of a site when a message is placed on an AzureQueue. I'm using CertificateCloudeCredentials to authenticate the request. I'm creating the credentials with the following code:

var certificateString = "<the certificate string>";
var certificateString = ConfigurationManager.AppSettings["Base64Certificate"];
var certificate = new X509Certificate2(Convert.FromBase64String(certificateString));
var credentials = new CertificateCloudCredentials("67baa805-e391-4e9a-a26e-aa76d33f6475", certificate);
var managementClient =  new WebSiteManagementClient(credentials);

On my development machine this works fine, but when i upload the WebJob on azure, i get an exception when i try to make a request with the management client instance. The exception message says: "Message:ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription."

Apparently i need to have the certificate installed on the machine that will run the code, but since this is a WebJob running on an Azzure WebApplication, how could i achieve this?

The other authentication option for the management API is using TokenCredentials, but i couldn't make those works on my local environment. I've tried this guide to no avail https://msdn.microsoft.com/en-us/library/dn722415.aspx . Also i doubt it will work since you need to log on with an Azzure account in order to get the token.

So.. ¿How can i use the management APIs from within a WebJob?

andyroschy
  • 499
  • 3
  • 11

1 Answers1

3

I think Token Credentials is the better way to go. I have a full sample here that you can use for inspiration. You will need to set up a Service Principal, which you can give to your app via Azure App Settings.

David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • Thanks a lot for the answer, the code sample and specially the blog post referenced on the app.config of the sample were of great help. I'm still geting a forbidden exception with the token credentials, but i'm guessing that's because i've set delegate permissons for the ManagementApi to the AD app. I'm waiting for the administrator to set the application permissions to see if it works then. – andyroschy Mar 11 '16 at 18:12