CustomRequestCultureProvider:context.User.Identity.IsAuthenticated 总是假的

Mét*_*ule 2 asp.net-core asp.net-core-localization asp.net-core-identity

我正在尝试CustomRequestCultureProvider在我的本地化管道中实现 a ,以从数据库中检索用户文化。为了识别用户,我需要ClaimsPrincipal,但由于某种原因,即使用户已通过身份验证,context.User.Identity.IsAuthenticated也始终为 false。

这是相关代码(被截断以突出我的问题:身份验证已经在工作;本地化代码基于本地化教程)。

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("fr"),
                new CultureInfo("en"),
            };

            options.DefaultRequestCulture = new RequestCulture("fr");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;

            // we insert at 2, because we want to first check the query, then the cookie, then the DB, then the headers
            options.RequestCultureProviders.Insert(2, new CustomRequestCultureProvider(async context =>
            {
                // ----->> ISSUE IS HERE: this is always false!
                if (context.User.Identity.IsAuthenticated)
                    return new ProviderCultureResult("en");

                return null;
            }));
        });

        services
            .AddLocalization(options => options.ResourcesPath = "Resources")
            .AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            ;

        return services;
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseRequestLocalization();
        app.UseStaticFiles();
        app.UseAuthentication();
        app.UseMvcWithDefaultRoute();
        return app;
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能检索ClaimsPrincipal我的CustomRequestCultureProvider?

注意:更改注册顺序(app.UseAuthentification之前调用app.UseRequestLocalization不能解决问题)。

New*_*ewm 8

以防万一将来有人遇到这个问题,我在 ASP.NET Core 2.1 中遇到了类似的问题,结果我在插入CustomRequestCultureProvider. 为了修复它,我移到了app.UseAuthentication() 上面options.RequestCultureProviders.Insert(...