Nagarajan B P
Nagarajan B P

Reputation: 169

Call Azure API from WebJob/ Shared code between WebJob and web API

I have a web API in an ASE and an associated web job. I am trying to call this web API from the web job but it always fails with:

winhttpexception: a security error has occurred.

I have put in all the tls related settings but still getting the error. Any suggestions on the error? Also is there a way to share code between WebJob and web API?

Upvotes: 1

Views: 564

Answers (2)

Nagarajan B P
Nagarajan B P

Reputation: 169

I was able to resolve the issue by setting the below in my code.This resolved the Security Error.

using(var handler = new HttpClientHandler
{
   ServerCertificateCustomValidationCallback = (sender,certificate,chain,sslPolicyErrors) => true
})

Upvotes: 1

Joey Cai
Joey Cai

Reputation: 20067

You could create a console app and publish it as Azure WebJobs. For username and password you could click Get Publish Profile in your Azure webapp overview to get them. enter image description here

Then you could use the following code in Console App to call your Azure Webapi.

string userName = "$xxxxxx";
string userPassword = "xxxxxxxxxxxxx";
string webAppName = "xxxxxx";
var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{userName}:{userPassword}"));
using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64Auth);
    var baseUrl = new Uri($"https://{webAppName}.azurewebsites.net/api/values");
    var result = client.GetAsync(baseUrl).Result;
    if (result.IsSuccessStatusCode)
    {
        var readTask = result.Content.ReadAsStringAsync();
        readTask.Wait();
        var value = readTask.Result;
        Console.WriteLine(value.ToString());
    }
}
Console.WriteLine("run successfully");

Output as below: enter image description here

Upvotes: 0

Related Questions