Search code examples
c#authenticationoauth-2.0azure-functionsbearer-token

Azure Functions root URL authentication with bearer token/OAuth2


I have created a Functions app. Assume that the app's root URL is: https://mycompanyportal.azurewebsites.net. I want to authenticate the root URL and return "401 unauthorized" when it is hit without an appropriate token from a browser or anywhere.

Please note that I can already successfully authenticate(bearer/OAuth2) the requests to different functions(but not root URL) hosted in the app. Lets say if "Default" is one of the functions in my app then I am able to authenticate the request when the URL(https://mycompanyportal.azurewebsites.net/api/Default) is called. I have done this with the help of validator method that is called in a if condition, when the control hits the first line of the functions code.

Please let me know how to authenticate a Function app's root URL using bearer/OAuth2 validation in code. TIA


Solution

  • One way to do it is by using Function proxies and re-routing the root URL to a particular function, where the bearer token validation is configured. In your proxies.json you can add the below json. So, when you are hitting "https://example.com"(the root URL) it will be re-routed to your specified function with bearer token auth enabled.

    #proxies.json
        {
          "proxies": {
            "EmptyRouteProxy": {
              "matchCondition": {
                "route": "/"
              },
              "backendUri": "http://localhost:7071/api/SampleFunc"
            }
          }
        }