Search code examples
c#azurepostazure-functionshttp-status-code-403

Forbidden when I try to POST to Azure function


I have daployed an Azure function.

The function application is secured by Azure Active Directory, so in order to call it I have to log in using my AAD credentials.

And everything is fine when I make a GET call to that function.

But when I try to make a POST call, I'm receiving following error: 403 Forbidden You do not have permission to view this directory or page.

The function is deployed using Visual Studio and its entry point looks like this:

public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(
        AuthorizationLevel.Anonymous, 
        "post", "get", 
        Route = null)] HttpRequestMessage req, 
    TraceWriter log)
{
    ...
}

Solution

  • Its working fine for me and below are the steps what I did:

    1. Deployed azure function v1 from Visual studio with the below code:
    [FunctionName("Function1")]
            public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
            {
                log.Info("C# HTTP trigger function processed a request.");
    
                // parse query parameter
                string name = req.GetQueryNameValuePairs()
                    .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                    .Value;
    
                if (name == null)
                {
                    // Get request body
                    dynamic data = await req.Content.ReadAsAsync<object>();
                    name = data?.name;
                }
    
                return name == null
                    ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
                    : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
            }
    
    1. Enable EasyAUth from Portal using AAD.

    EasyAuthImage

    1. Get the ClientID and Client Secret of your AAD application from the Authentication / Authorization Blade.

    ClientID/ClientSceret

    1. Get the Token using below Postman call:

    Postman

    1. Once you get the token from the postman as shown above, make a call to azure function using the same token shown below:

    FunctionCall