CustomRequestCultureProvider:context.User.Identity.IsAuthenticated始终为false [英] CustomRequestCultureProvider: context.User.Identity.IsAuthenticated is always false

查看:91
本文介绍了CustomRequestCultureProvider:context.User.Identity.IsAuthenticated始终为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这里是相关代码(被截断以突出我的问题:身份验证已在起作用;本地化代码基于 公共类启动{公共无效ConfigureServices(IServiceCollection服务){services.Configure< RequestLocalizationOptions>(options =>{varsupportedCultures = new []{新的CultureInfo("fr"),新的CultureInfo("en"),};options.DefaultRequestCulture =新的RequestCulture("fr");options.SupportedCultures = supportCultures;options.SupportedUICultures = supportCultures;//我们在2处插入,因为我们要先检查查询,然后检查cookie,然后检查DB,然后再检查标题options.RequestCultureProviders.Insert(2,新的CustomRequestCultureProvider(异步上下文=>{//----->>问题在这里:这始终是错误的!如果(context.User.Identity.IsAuthenticated)返回新的ProviderCultureResult("en");返回null;}));});服务.AddLocalization(options => options.ResourcesPath =资源").AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);退货服务;}公共无效的Configure(IApplicationBuilder应用程序,IHostingEnvironment env,ILoggerFactory loggerFactory){app.UseRequestLocalization();app.UseStaticFiles();app.UseAuthentication();app.UseMvcWithDefaultRoute();返回应用程序;}}

我该如何在我的 CustomRequestCultureProvider 中检索 ClaimsPrincipal ?

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

我设法通过手动调用身份验证管道来使其工作.我怀疑我需要这样做,因为 Authorize 属性是在中间件管道中稍后触发的,因此现在检索 ClaimsPrincipal (现在可能是错误的)还为时过早./p>

无论如何,这是我修改 CustomRequestCultureProvider 的方法:

 //我们在2处插入,因为我们要先检查查询,然后是cookie,然后是DB,然后是标题options.RequestCultureProviders.Insert(2,新的CustomRequestCultureProvider(异步上下文=>{//从Cookie中检索ClaimsPrincipal//注意:这取决于身份验证的设置方式var authResult =等待context.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);如果(!authResult.成功)返回null;var user = authResult.Principal;如果(用户!= null)返回新的ProviderCultureResult("en");返回null;})); 

I'm trying to implement a CustomRequestCultureProvider in my localization pipeline, to retrieve the user culture from the database. To identify the user, I need the ClaimsPrincipal, but for some reason, even though the user is authenticated, context.User.Identity.IsAuthenticated is always false.

Here's the relevant code (truncated to highlight my issue: authentication is already working; localization code is based on the localization tutorial).

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;
    }
}

What should I do to retrieve the ClaimsPrincipal in my CustomRequestCultureProvider?

Note: changing the registration order (calling app.UseAuthentification before app.UseRequestLocalization doesn't fix the issue).

解决方案

I managed to make it work by manually calling the authentication pipeline. I suspect I need to do that because the Authorize attribute is triggered later in the middleware pipeline, and thus it's too early to retrieve the ClaimsPrincipal (I might be wrong).

In any case, here's how I modified my CustomRequestCultureProvider:

// 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 =>
{
    // retrieve the ClaimsPrincipal from the cookies
    // note: this is dependent on how your authentication is set up
    var authResult = await context.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    if (!authResult.Succeeded)
        return null;

    var user = authResult.Principal;
    if (user != null)
        return new ProviderCultureResult("en");

    return null;
}));

这篇关于CustomRequestCultureProvider:context.User.Identity.IsAuthenticated始终为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆