Skip to content

Using dotnet auth with SnapStart under dev env with minimal api and AspNetCoreServer.Hosting throws exception #2236

@McDoit

Description

@McDoit

Describe the bug

Adding SnapStart to a lambda that is using AspNetCoreServer.Hosting with warmup calls via AddAWSLambdaBeforeSnapshotRequest fails

Using dotnet auth with SnapStart with minimal api and AspNetCoreServer.Hosting throws exception
System.InvalidOperationException: Cannot resolve scoped service Microsoft.AspNetCore.Authentication.IAuthenticationHandlerProvider' from root provider.

For some reason its triggered in DeveloperExceptionPageMiddleware, and only shows up in the Development aspnet_environment setting

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

To not throw on warmup

Current Behavior

Throws this exception:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Cannot resolve scoped service 'Microsoft.AspNetCore.Authentication.IAuthenticationHandlerProvider' from root provider.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(ServiceCallSite callSite, IServiceScope scope, IServiceScope rootScope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Reproduction Steps

This code as minimal api lambda, created with the serverless.MinimalApi dotnet template:

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;

var builder = WebApplication.CreateBuilder(args);

var key = System.Text.Encoding.ASCII.GetBytes("YourSecretKeyHere");

builder.Services.AddAuthentication(x =>
{
    x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
    x.RequireHttpsMetadata = false;
    x.SaveToken = true;
    x.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuerSigningKey = true,
        IssuerSigningKey = new SymmetricSecurityKey(key),
        ValidateIssuer = false,
        ValidateAudience = false
    };
});

builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);

builder.Services.AddAWSLambdaBeforeSnapshotRequest(new HttpRequestMessage(HttpMethod.Get, "/"));

var app = builder.Build();

app.UseAuthentication();

app.MapGet("/", () => "Welcome to running ASP.NET Core Minimal API on AWS Lambda");

app.Run();

Needs these packages:

<PackageReference Include="Amazon.Lambda.AspNetCoreServer.Hosting" Version="1.9.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.22" />

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Amazon.Lambda.AspNetCoreServer.Hosting 1.9.0

Targeted .NET Platform

.NET 8

Operating System and version

AmazonLinux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.needs-reproductionThis issue needs reproduction.response-requestedWaiting on additional info and feedback. Will move to close soon in 7 days.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions