简体   繁体   中英

Using Azure Management API from AzureWebJob

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM