FuncAuthz 1.0.1

dotnet add package FuncAuthz --version 1.0.1
                    
NuGet\Install-Package FuncAuthz -Version 1.0.1
                    
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="FuncAuthz" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FuncAuthz" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="FuncAuthz" />
                    
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 FuncAuthz --version 1.0.1
                    
#r "nuget: FuncAuthz, 1.0.1"
                    
#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 FuncAuthz@1.0.1
                    
#: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=FuncAuthz&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=FuncAuthz&version=1.0.1
                    
Install as a Cake Tool

FuncAuthz

FuncAuthz is a .NET package designed to provide authentication and authorization for Azure Function Apps using JWT tokens. This package simplifies the process of securing your Azure Functions by integrating JWT token validation and role-based access control.


Features

  • JWT token validation
  • Role-based access control
  • Policy-based authorization
  • Middleware integration for Azure Functions

Installation

To install FuncAuthz, add the package to your project using NuGet:

dotnet add package FuncAuthz

Usage

Configuration

1. Configure Services

In your Program.cs or Startup.cs, configure the authentication and authorization services:

using System.Text;
using FuncAuthz.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication(builder =>
    {
        builder.AddAuthentication()
            .AddJwtBearer(new TokenValidationParameters
            {
                ValidateAudience = false,
                ValidateIssuer = true,
                ValidIssuer = "Issuer",
                RequireExpirationTime = true,
                IssuerSigningKey =
                    new SymmetricSecurityKey(
                        Encoding.UTF8.GetBytes("A secure key that's shared between AspNetCore and Azure Functions")),
                ValidateIssuerSigningKey = true,
                ValidateLifetime = true,
                ClockSkew = TimeSpan.Zero
            });
    })
.Build();

host.Run();
2. Add Middleware

Ensure the AuthorizationMiddleware is added to the pipeline:

builder.UseMiddleware<AuthorizationMiddleware>();

Applying Authorization

1. Authorize Function

Use the [Authorize] attribute to secure your Azure Functions:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.AspNetCore.Authorization;
using System.Net;

public class MyFunction
{
    [Function("MyFunction")]
    [Authorize(Roles = "Admin")]
    public async Task<HttpResponseData> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
        FunctionContext executionContext)
    {
        var response = req.CreateResponse(HttpStatusCode.OK);
        await response.WriteStringAsync("Hello, authorized user!");
        return response;
    }
}
2. Allow Anonymous Access

Use the [AllowAnonymous] attribute to allow anonymous access to specific functions:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.AspNetCore.Authorization;
using System.Net;

public class MyFunction
{
    [Function("MyFunction")]
    [AllowAnonymous]
    public async Task<HttpResponseData> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
        FunctionContext executionContext)
    {
        var response = req.CreateResponse(HttpStatusCode.OK);
        await response.WriteStringAsync("Hello, anonymous user!");
        return response;
    }
}

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.


License

This project is licensed under the MIT License. See the LICENSE file for details.


Acknowledgements

  • Microsoft IdentityModel
  • Azure Functions

Contact

For any questions or feedback, please contact the project maintainers.


Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.1 184 11/23/2024
1.0.0 163 11/23/2024
0.1.0.3 182 11/22/2024
0.1.0.2 162 11/22/2024
0.1.0.1 159 11/22/2024