dark-loop / functions-authorize

An ASP.NET Core based authentication and authorization middleware for HTTP triggered Azure Functions (In-Proc and Isolated)
Apache License 2.0
40 stars 5 forks source link

Azure Functions Not Appearing in Portal When Using [FunctionAuthorize] Attribute and Authentication Configuration in Startup.cs #70

Open togosh opened 1 month ago

togosh commented 1 month ago

We are encountering an issue where our Azure Functions do not appear in the Azure Portal's Function App Functions list when we apply the [FunctionAuthorize] attribute from the DarkLoop.Azure.Functions.Authorize package and include authentication and authorization configuration in our Startup.cs file.

Environment Details:

Azure Functions Version:    v4 Target Framework:    .NET 8 (net8.0) Functions Runtime:    In-Process DarkLoop.Azure.Functions.Authorize Version:    3.1.2 and 4.0.0 Deployment Method:    Azure DevOps YAML pipeline Hosting Environment:    Azure Function App

Problem Description:

When we include the following authentication and authorization configuration in our Startup.cs along with the [FunctionAuthorize] attribute, the functions do not appear in the Azure Portal:

// Authentication & Authorization.
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

var serviceProvider = builder.Services.BuildServiceProvider();
var workflowSecrets = serviceProvider.GetService<WorkflowServerSecretsSettings>();

builder.Services
    .AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuer = false,
            ValidateAudience = false,
            ValidateLifetime = true,
            ValidIssuer = workflowSecrets.Jwt.Issuer,
            ValidAudience = workflowSecrets.Jwt.Audience,
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(workflowSecrets.Jwt.SecretKey)),
            NameClaimType = JwtRegistered ClaimNames.Sub
        };
    });

builder.Services.AddAuthorization(auth => auth.AddSecurityPolicy());

When we remove this code and the [FunctionAuthorize] attribute from our functions, they appear in the Azure Portal as expected.

Functionality:

Question:

artmasa commented 1 month ago

At the moment there's a problem loading the right assemblies for this framework when using .NET 8 for the In-Process mode. Issue #58 explains what is going on. Host and project should depend on same ASP.NET Core authentication packages to succeed.