...

/

Applying Different Authorization Levels

Applying Different Authorization Levels

Learn how to apply different authorization levels in Azure Functions to allow anonymous access, function key access, and admin-only access.

In this lesson, we will learn how to apply different types of authorization to our functions. We do so with the aid of the interactive playground below. This playground represents a function app that has three HTTP endpoints, each demonstrating its own level of authorization.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace AuthenticationExample;

public static class Functions
{
    [FunctionName("AnonymousAccess")]
    public static async Task<IActionResult> AnonymousAccess(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("Anonymous function triggered.");
        return new OkResult();
    }

    [FunctionName("FunctonLevelAccess")]
    public static async Task<IActionResult> FunctonLevelAccess(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("Function triggered with function-level authorization.");
        return new OkResult();
    }

    [FunctionName("AppAdminAccess")]
    public static async Task<IActionResult> AppAdminAccess(
        [HttpTrigger(AuthorizationLevel.Admin, "get", "post")] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("Function triggered with admin-level authorization.");
        return new OkResult();
    }
}
Authorization examples in Azure Functions

Note: Access levels only apply to functions ...