0

I have created an azure function app and created few functions into it. I wanted to check if the user is authorized to access the route or not (from my DB). I need to check this before the request execution. How can I achieve this in function app?

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
MILJO JOHN
  • 31
  • 5

2 Answers2

0

If you're using function v2 with c#, then you can write your own Startup class and register it with your function. The Startup class always executes prior to the function executes.

Please also make sure you're using the latest version of Microsoft.NET.Sdk.Functions nuget package(the latest version is 1.0.29 as of now)

Here is a sample code, and please feel free to modify it as per your need:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.Logging;
using System.IO;

[assembly: WebJobsStartup(typeof(FunctionApp16.MyStartup))]
namespace FunctionApp16
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run("your parameters")
        {
            //your code here
        }
    }

    public class MyStartup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            //write your code here, it will executes prior to the function method.
        }
    }

}
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
0

Azure API Management services enables JWT validation on all incoming requests.

The validate-jwt policy enforces existence and validity of a JWT extracted from either a specified HTTP Header or a specified query parameter.

Among enforcing other security and authorization use cases, this is very useful for checking for permissions in claims. You can read more in the documentation here. While this won't query your DB for user permissions, it is a very effective way to achieve your goal.

Here's the syntax from the documentation.

  <required-claims>
    <claim name="name of the claim as it appears in the token" match="all|any" separator="separator character in a multi-valued claim">
      <value>claim value as it is expected to appear in the token</value>
      <!-- if there is more than one allowed values, then add additional value elements -->
    </claim>
    <!-- if there are multiple possible allowed values, then add additional value elements -->
  </required-claims>

Here's how you could implement it.

  <required-claims>
    <claim name="FirstRoute" match="any">
      <value>true</value>
    </claim>
    <claim name="SecondRoute" match="any">
      <value>false</value>
    </claim>
  </required-claims>
TheCascadian
  • 485
  • 3
  • 14