AzureFunctions.Authentication
6.0.1
dotnet add package AzureFunctions.Authentication --version 6.0.1
NuGet\Install-Package AzureFunctions.Authentication -Version 6.0.1
<PackageReference Include="AzureFunctions.Authentication" Version="6.0.1" />
paket add AzureFunctions.Authentication --version 6.0.1
#r "nuget: AzureFunctions.Authentication, 6.0.1"
// Install AzureFunctions.Authentication as a Cake Addin #addin nuget:?package=AzureFunctions.Authentication&version=6.0.1 // Install AzureFunctions.Authentication as a Cake Tool #tool nuget:?package=AzureFunctions.Authentication&version=6.0.1
AzureFunctions.Authentication
Provides Azure Functions friendly ASP.NET Core Authentication/Authorization
https://www.nuget.org/packages/AzureFunctions.Authentication
Problem
Azure Function have ability to use Startup.cs class with Dependecy Injection same as ASP.NET Core applications, it is not working as expected out of the box though. The reason for that is default/internal (admin) features of Azure Functions Web Host that are protected with the exact same ASP.NET Core Authentication/Authorization registrations which will be overriden once you register yours and you'll start seeing such problem in Azure Portal .
Moreover, predefined Azure Functions AuthorizationLevel
policies wont work neither.
Root cause of a problem
Please read this issue to get more context: https://github.com/Azure/azure-functions-host/issues/6805.
Workaround
Register ASP.NET Core Authentication/Authorization in such a way that it is not replacing nor dropping existing configurations/schemas/handlers but extend it instead. This is achieved with "Dynamic schema registration", see example here: https://github.com/aspnet/AuthSamples/tree/master/samples/DynamicSchemes.
Solution
- Expose custom Authentication/Authorization builder extensions that dont override existing one but registers all needed services
- Provide custom extension that derives from
IExtensionConfigProvider
- Re-configure already configured Authentication/Authorization by Azure Functions
- Dynamically inject new authentication schema and handler since
Bearer
schema is used by Azure Functions with their handler - Override
IAuthorizationHandlerProvider
to merge Azure Functions handlers with application handlers
Example
- Add/replace existing AddAuthentication/AddAuthorization extension methods to AddFunctionAuthentication/AddFunctionAuthorization
- Register
IAuthorizationHandler
handlers - Inject all needed services
IAuthenticationSchemeProvider/IAuthorizationPolicyProvider/IPolicyEvaluator
to authenticate & authorize request inside function. Alternative, encourage you to try nice package out there to simplify this process https://www.nuget.org/packages/DarkLoop.Azure.Functions.Authorize - Enjoy 😄
Important
! Do not try to use the "Bearer" scheme name since it is already used by AzureFunction host internally, provide any other name like: "CustomBearer", "B2B", etc.
Code snippet
private static void ConfigureAuthorization(IFunctionsHostBuilder builder)
{
var configuration = builder.GetContext().Configuration;
builder.Services.AddFunctionAuthentication(
configuration,
defaultAuthenticationScheme: "CustomBearer");
builder.Services.AddFunctionAuthorization(
configuration,
configureDefaultPolicy: policy => policy.RequireRole("Admin"),
configureOptions: options =>
{
options.AddPolicy(
"Organizatiuon",
builder => builder
.Combine(options.DefaultPolicy)
.AddRequirements(new OrganizationRequirement()));
});
builder.Services.AddScoped<IAuthorizationHandler, OrganizationAuthorizationHandler>();
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.0
- Microsoft.Azure.WebJobs (>= 3.0.39)
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 |
---|---|---|
6.0.1 | 1,361 | 6/6/2024 |
6.0.0 | 139 | 6/4/2024 |
4.1.0 | 11,394 | 5/25/2023 |
4.0.7.3 | 161 | 5/25/2023 |
4.0.7.2 | 142 | 5/25/2023 |
4.0.7.1 | 154 | 5/25/2023 |
4.0.7 | 6,895 | 2/23/2023 |
4.0.6 | 1,052 | 1/24/2023 |
4.0.5 | 3,841 | 5/21/2022 |
4.0.3 | 489 | 4/27/2022 |
4.0.2 | 449 | 4/23/2022 |
4.0.1 | 446 | 4/22/2022 |
4.0.0 | 567 | 4/22/2022 |
1.0.6 | 5,230 | 9/2/2021 |
1.0.5 | 353 | 9/2/2021 |
1.0.4 | 339 | 8/31/2021 |
1.0.3 | 360 | 8/23/2021 |
1.0.2 | 319 | 8/23/2021 |
1.0.1 | 334 | 8/23/2021 |
1.0.0 | 362 | 8/23/2021 |
- Migrated to .net6
- Added Source Link
- Added crutch to support new script host startup