NullReferenceException in Microsoft.AspNetCore.Http.DefaultHttpContext.get_User()
Hello,
We randomly experience exception System.NullReferenceException: Object reference not set to an instance of an object at Microsoft.AspNetCore.Http.DefaultHttpContext.get_User(), full stack below.
Two custom middleware’s are only read header data and store them in HttpContext Items.
I'm not able to reproduce this issue in testing or dev environment.
We have two servers deployment in "farm" and using Redis for Distributed cache and for DataProtection Keys.
May be it's similar issue as #2799
running on premise as IIS application OS: Windows Server 2012 R2, 6.3.9600
dotnet: Host (useful for support): Version: 2.1.6 Commit: 3f4f8eebd8
.NET Core SDKs installed: No SDKs were found.
.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.DefaultHttpContext.get_User()
at Microsoft.AspNetCore.Authorization.Policy.PolicyEvaluator.AuthenticateAsync(AuthorizationPolicy policy, HttpContext context)
at Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter.OnAuthorizationAsync(AuthorizationFilterContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext)
at Company.Client.Middleware.SsoTokenAuthMiddleware.Invoke(HttpContext httpContext) in d:\Client\Middleware\SsoTokenAuthMiddleware.cs:line 35
at Company.Client.Middleware.IsOutageChecker.Invoke(HttpContext httpContext) in d:\Client\Middleware\IsOutageChecker.cs:line 38
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context)
at NWebsec.AspNetCore.Middleware.Middleware.CspMiddleware.Invoke(HttpContext context)
at NWebsec.AspNetCore.Middleware.Middleware.MiddlewareBase.Invoke(HttpContext context)
at Company.Client.Middleware.HttpHeaderParserMiddleware.Invoke(HttpContext httpContext) in d:\Client\Middleware\HttpHeaderParserMiddleware.cs:line 50
at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
I'm closing, our fault. After we upgrade our project from 2.1 to 2.2. same exception appears in colleague computer. Then we found that we have background thread in our NLog custom target and we render message in that background thread, it access HttpContext. After we move message rendering from background thread exception disappear.