DarkLoop.Azure.Functions.Authorize 3.1.0

.NET Standard 2.1
Install-Package DarkLoop.Azure.Functions.Authorize -Version 3.1.0
dotnet add package DarkLoop.Azure.Functions.Authorize --version 3.1.0
<PackageReference Include="DarkLoop.Azure.Functions.Authorize" Version="3.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DarkLoop.Azure.Functions.Authorize --version 3.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DarkLoop.Azure.Functions.Authorize, 3.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install DarkLoop.Azure.Functions.Authorize as a Cake Addin
#addin nuget:?package=DarkLoop.Azure.Functions.Authorize&version=3.1.0

// Install DarkLoop.Azure.Functions.Authorize as a Cake Tool
#tool nuget:?package=DarkLoop.Azure.Functions.Authorize&version=3.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

functions-authorize

Bringing AuthorizeAttribute Behavior to Azure Functions v3

It hooks into .NET Core dependency injection container to enable authentication and authorization in the same way ASP.NET Core does.

License

This projects is open source and may be redistributed under the terms of the Apache 2.0 license.

Using the package

Installing the package

dotnet add package DarkLoop.Azure.Functions.Authorize

Setting up authentication

The goal is to utilize the same authentication framework provided for ASP.NET Core

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using MyFunctionAppNamespace;

[assembly: FunctionsStartup(typeof(Startup))]
namespace MyFunctionAppNamespace
{
  class Startup : FunctionsStartup
  {
    public void Configure(IFunctionsHostBuilder builder)
    {
      builder
        .AddAuthentication(options =>
        {
          options.DefaultAuthenticationScheme = JwtBearerDefaults.AuthenticationScheme;
          options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddOpenIdConnect(options =>
        {
          options.ClientId = "<my-client-id>";
          // ... more options here
        })
        .AddJwtBearer(options =>
        {
          options.Audience = "<my-audience>";
          // ... more options here
        });

      builder
        .AddAuthorization(options =>
        {
          options.AddPolicy("OnlyAdmins", policyBuilder =>
          {
            // configure my policy requirements
          });
        });
    }
  }
}

No need to register the middleware the way we do for ASP.NET Core applications.

Using the attribute

And now lets use FunctionAuthorizeAttribute the same way we use AuthorizeAttribute in our ASP.NET Core applications.

public class Functions
{
  [FunctionAuthorize]
  [FunctionName("get-record")]
  public async Task<IActionResult> GetRecord(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
    ILogger log)
  {
    var user = req.HttpContext.User;
    var record = GetUserData(user.Identity.Name);
    return new OkObjectResult(record);
  }

  [FunctionAuthorize(Policy = "OnlyAdmins")]
  [FunctionName("get-all-records")]
  public async Task<IActionResult>(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
    ILogger log)
  {
    var records = GetAllData();
    return new OkObjectResult(records);
  }
}

Releases

Nuget

Builds

master build status

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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
3.1.0 18,304 12/6/2021
3.1.0-preview-211123-1 5,153 11/23/2021
3.0.12-preview-211118-1 196 11/18/2021
3.0.12-preview-210730-1 376 7/30/2021
3.0.11-preview-210408-4 2,111 4/8/2021
3.0.11-preview-210408-3 109 4/8/2021
3.0.11-preview-210408-2 121 4/8/2021
3.0.11-preview-210408-1 122 4/8/2021
3.0.11-preview-210407-11 209 4/7/2021