-
Notifications
You must be signed in to change notification settings - Fork 391
Null Reference Exception | PostConfigureApplicationCookieTicketStore #1689
Description
Which version of Duende IdentityServer are you using?
7.1.0-rc.1
Which version of .NET are you using?
.NET 9
Describe the bug
After upgrading I am experiencing a null reference exception on startup. After looking at the changes, and debugging the PostConfigureApplicationCookieTicketStore locally I can see that the _licenseUsage is set to null in the constructor due to the httpContextAccessor being null. I do have the line in service registration that adds the HttpContextAccessor to the service provider, but am assuming this is null due to the the constructor being executed shortly after app.RunAsync() is called and there is no active HTTP request yet.
public PostConfigureApplicationCookieTicketStore(
IHttpContextAccessor httpContextAccessor,
IdentityServerOptions identityServerOptions,
IOptions<Microsoft.AspNetCore.Authentication.AuthenticationOptions> options,
ILogger<PostConfigureApplicationCookieTicketStore> logger)
{
// The HTTP context accessor is null, and thus _licenseUsage is null. The constructor is only called once.
_httpContextAccessor = httpContextAccessor;
_licenseUsage = httpContextAccessor.HttpContext?.RequestServices.GetRequiredService<LicenseUsageTracker>();
_logger = logger;
_scheme = identityServerOptions.Authentication.CookieAuthenticationScheme ??
options.Value.DefaultAuthenticateScheme ??
options.Value.DefaultScheme;
}
To Reproduce
Upgrade to 7.1.0-rc.1, fix compile errors (if any) and startup the application. I have a combination of the Entity Framework and ASP.NET Identity quickstart.
Expected behavior
Expected app to start up without errors, and allow navigation around the home/index page, login page, and all other ASP.NET identity related pages.
Log output/exception with stacktrace
An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
Duende.IdentityServer.Configuration.PostConfigureApplicationCookieTicketStore.PostConfigure(string name, CookieAuthenticationOptions options) in PostConfigureApplicationCookieTicketStore.cs, line 72
NullReferenceException: Object reference not set to an instance of an object.
Duende.IdentityServer.Configuration.PostConfigureApplicationCookieTicketStore.PostConfigure(string name, CookieAuthenticationOptions options) in PostConfigureApplicationCookieTicketStore.cs
Microsoft.Extensions.Options.OptionsFactory<TOptions>.Create(string name)
System.Lazy<T>.ViaFactory(LazyThreadSafetyMode mode)
System.Lazy<T>.ExecutionAndPublication(LazyHelper executionAndPublication, bool useDefaultConstructor)
System.Lazy<T>.CreateValue()
Microsoft.Extensions.Options.OptionsCache<TOptions>.GetOrAdd<TArg>(string name, Func<string, TArg, TOptions> createOptions, TArg factoryArgument)
Microsoft.Extensions.Options.OptionsMonitor<TOptions>.Get(string name)
Microsoft.AspNetCore.Authentication.AuthenticationHandler<TOptions>.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, string authenticationScheme)
Duende.IdentityServer.Hosting.FederatedSignOut.FederatedSignoutAuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, string authenticationScheme) in FederatedSignoutAuthenticationHandlerProvider.cs
Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, string scheme)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Duende.IdentityServer.Hosting.DynamicProviders.DynamicSchemeAuthenticationMiddleware.Invoke(HttpContext context) in DynamicSchemeAuthenticationMiddleware.cs
Duende.IdentityServer.Hosting.BaseUrlMiddleware.Invoke(HttpContext context) in BaseUrlMiddleware.cs
Enterprise.Applications.AspNetCore.Middleware.IgnoreFavicon.IgnoreFaviconMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+InterfaceMiddlewareBinder+<>c__DisplayClass2_0+<<CreateMiddleware>b__0>d.MoveNext()
Swashbuckle.AspNetCore.ReDoc.ReDocMiddleware.Invoke(HttpContext httpContext)
Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+InterfaceMiddlewareBinder+<>c__DisplayClass2_0+<<CreateMiddleware>b__0>d.MoveNext()
Enterprise.Middleware.AspNetCore.ConfigurableOptions.ConfigurableOptionsEndpointMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+InterfaceMiddlewareBinder+<>c__DisplayClass2_0+<<CreateMiddleware>b__0>d.MoveNext()
Enterprise.Middleware.AspNetCore.RegisteredServices.RegisteredServicesEndpointMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+InterfaceMiddlewareBinder+<>c__DisplayClass2_0+<<CreateMiddleware>b__0>d.MoveNext()
Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
System.NullReferenceException: Object reference not set to an instance of an object.
at Duende.IdentityServer.Configuration.PostConfigureApplicationCookieTicketStore.PostConfigure(String name, CookieAuthenticationOptions options) in /_/src/IdentityServer/Configuration/DependencyInjection/PostConfigureApplicationCookieTicketStore.cs:line 72
at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy`1.CreateValue()
at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd[TArg](String name, Func`3 createOptions, TArg factoryArgument)
at Microsoft.Extensions.Options.OptionsMonitor`1.Get(String name)
at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
at Duende.IdentityServer.Hosting.FederatedSignOut.FederatedSignoutAuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme) in /_/src/IdentityServer/Hosting/FederatedSignOut/FederatedSignoutAuthenticationHandlerProvider.cs:line 33
at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Duende.IdentityServer.Hosting.DynamicProviders.DynamicSchemeAuthenticationMiddleware.Invoke(HttpContext context) in /_/src/IdentityServer/Hosting/DynamicProviders/DynamicSchemes/DynamicSchemeAuthenticationMiddleware.cs:line 51
at Duende.IdentityServer.Hosting.BaseUrlMiddleware.Invoke(HttpContext context) in /_/src/IdentityServer/Hosting/BaseUrlMiddleware.cs:line 27
at Enterprise.Applications.AspNetCore.Middleware.IgnoreFavicon.IgnoreFaviconMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>c__DisplayClass2_0.<<CreateMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Swashbuckle.AspNetCore.ReDoc.ReDocMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>c__DisplayClass2_0.<<CreateMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Enterprise.Middleware.AspNetCore.ConfigurableOptions.ConfigurableOptionsEndpointMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>c__DisplayClass2_0.<<CreateMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Enterprise.Middleware.AspNetCore.RegisteredServices.RegisteredServicesEndpointMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>c__DisplayClass2_0.<<CreateMiddleware>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
Additional context
I have gone through the breaking changes section in the release and updated the constructor of my custom / extended configuration store DB context class, and namespaces from IdentityModel to Duende.IdentityModel. I haven't changed anything else except for the other Duende packages for ASP.NET Identity and Entity Framework Core.
I do have some additional cookie option related calls in my code such as services.ConfigureApplicationCookie(). I've tried commenting these out but they haven't seemed to have an effect. You might notice the other middleware listed in the stack trace. Those are only enabled in the dev environment and are mapped to specific paths that aren't being hit.
Hope this helps. I may have something weird in my startup code that is affecting it, but figured I'd create an issue and start the discussion. Its worth noting that the previous 7.0.8 version had worked just fine, and I haven't made many major changes to the code. Let me know if you need any additional information.