Search code examples
.netazure-functionshttpclienthttp-status-code-401unauthorized

Not able to send http Request with authorization header in my Azure function when deployed in Server


I have used HttpClient and I'm setting Authorization headers to it, when I run my Timer Trigger and HTTP trigger functions locally Everything is working fine and able to get the expected response,

But, when I deployed the same in the production environment It is throwing 401 Unauthorized error in the response. I'm able to see the Authorization headers in HttpRequest Object through my logs, but the same is not reaching the destination end point.

Thank you.

I've tried Default headers, and reused the HttpClient

HttpClient client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes(configObj.loginId + ":" + configObj.password);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
               
 client.BaseAddress = new Uri(configObj.Url);

Then I'm passing the client object as parameter to some methods and using client as mentioned below

public static async Task<string> methodName(string VariableName, HttpClient client)
{
 HttpResponseMessage response = await client.GetAsync($"/reports/{VariableValue}");
 string content = await response.Content.ReadAsStringAsync();
    if (response.IsSuccessStatusCode)
    {
       playing with the Data
    }
}

I even Tried assigning all the values to HttpRequestmessage and send it through http client, even that didn't help me in the production.


Solution

  • Thankyou Asiful-Nobel. Posting your suggestion as an answer so that it will be helpful for other community members who face similar kind of issues.

    Below is the command which will help you in getting the authorization for function in production.

    https://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>?code=<API_KEY>
    

    For complete information check the link.