Birchsoft.Azure.Function.Identity 1.0.2

dotnet add package Birchsoft.Azure.Function.Identity --version 1.0.2
                    
NuGet\Install-Package Birchsoft.Azure.Function.Identity -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Birchsoft.Azure.Function.Identity" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Birchsoft.Azure.Function.Identity" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Birchsoft.Azure.Function.Identity" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Birchsoft.Azure.Function.Identity --version 1.0.2
                    
#r "nuget: Birchsoft.Azure.Function.Identity, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Birchsoft.Azure.Function.Identity@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Birchsoft.Azure.Function.Identity&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Birchsoft.Azure.Function.Identity&version=1.0.2
                    
Install as a Cake Tool

Birchsoft.Azure.Function.Identity


Important:

The NuGet package supports only Azure Functions using the isolated worker model and validates JWT issued by Microsoft Entra ID.


The Birchsoft.Azure.Function.Identity package simplifies Azure HTTP Trigger Function Authentication by utilizing JSON Web Tokens (JWT).

Configuration

To set up your function correctly in Azure, follow these steps:

1. Create a JSON configuration file and name it identity.settings.json:

{
    "AzureMEID": {
        "TenantId": "xxxxxxxx-8eba-45ca-a7af-xxxxxxxxxxxx",
        "Audience": "xxxxxxxx-2612-4413-b5b2-xxxxxxxxxxxx",
        "ObjectId": "xxxxxxxx-9f62-4343-aaf6-xxxxxxxxxxxx"
    }
}

The Audience is the Application (client) ID from Azure App Registrations.

You can choose any name for this JSON configuration file, but make sure to specify it as a parameter in the AddAzFunctionIdentityJsonConfig() method.

2. Configure the Program.cs file:

class Program
{
    public static async Task Main(string[] args)
    {
        var host = new HostBuilder()
            .ConfigureAppConfiguration((hostContext, configBuilder) =>
            {
                configBuilder.SetBasePath(hostContext.HostingEnvironment.ContentRootPath)
                        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                        .AddAzFunctionIdentityJsonConfig(hostContext)
                        .AddEnvironmentVariables();
            })
            .ConfigureFunctionsWebApplication(worker =>
            {
                worker.UseAzFunctionIdentityMiddleware(false);
            })
            .ConfigureServices((hostContext, services) =>
            {
                var configuration = hostContext.Configuration;

                services.AddLogging();
                services.AddAzFunctionIdentityConfig(configuration);
            })
            .Build();

        await host.RunAsync();
    }
}

3. Skip Azure function authorization

You can also bypass Azure Function (endpoint) authorization by adding the attribute [SkipFunctionAuthorization].

[SkipFunctionAuthorization]
[Function("FunctionNotAuth")]
public IActionResult RunNotAuth([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
    _logger.LogInformation("C# HTTP trigger function processed a request FunctionNotAuth.");
    return new OkObjectResult("Welcome to Azure FunctionNotAuth!");
}

4. Authorization based on roles included in the token.

To authorize functions based on the roles specified in the token, add an attribute, such as [AppRoles("Admin", "User")].


Important:

If the token contains roles and the function does not include the AppRoles attribute with the corresponding role, authentication will fail.


[AppRoles("Admin", "User")]
[Function("FunctionAuthRoles")]
public IActionResult RunAuthRoles([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
    _logger.LogInformation("C# HTTP trigger function processed a request FunctionAuthRoles.");
    return new OkObjectResult("Welcome to Azure FunctionAuthRoles!");
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 129 2/2/2025
1.0.1 106 1/19/2025
1.0.0 102 1/19/2025

Added an attribute to allow token authentication based on roles.