aspnetcore: Regression in net core 3.0: HttpContext.User.Identity.IsAuthenticated always false with custom RequestCultureProvider

From @jcemoller on Monday, September 9, 2019 1:54:37 PM

I’ve tried to migrate a net core 2.2 web application to net core 3.0 preview 9. I’m having troubles with middleware, where HttpContext.User.Identity.IsAuthenticated always returns false even if the user is authenticated. To ensure that I do not made any mistakes during migration of our application, I even tried to set up a new 3.0 web application project and follow the instructions on this page. In the code below IsAuthenticated is always false.

How do I make this work in 3.0? I have to check authenticated user to resolve the culture based on a SQL query.

                options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
                {
                    var isAuthenticated = context.User.Identity.IsAuthenticated; // always false in 3.0 preview9
                    // My custom request culture logic
                    return new ProviderCultureResult("en");
                }));

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Copied from original issue: aspnet/AspNetCore.Docs#14221

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 19 (16 by maintainers)

Most upvoted comments

@hishamco @mkArtakMSFT

I’ve now created a new project from a 2.2 template, see commit https://github.com/jcemoller/net-core-localization-bug/commit/b40367a30fb82d398f6803590b9b678e1c32548b.

In the repo code, if UseRequestLocalization is placed in LOCATION 1 you will always see the text “IsAuthenticated is FALSE” in Home/Index. If UseRequestLocalization is placed in LOCATION 2, after logging in the text will change to “IsAuthenticated is TRUE”.

I’m sure that the order could be reversed in my project before migrating to 3.0, but I cannot reproduce this with a clean repo from 2.2 template. It must be some other code in the actual project where I experienced the issue that made it working even though the order was reversed there.

Still, it would be great if the docs at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.2#localization-middleware were updated to reflect that in order to use IsAuthenticated, you cannot put it in the advised location but it has to be after UseAuthentication.

Thanks.