Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

425
Views
ASP.NET Core 5 MVC : httpContext.User not authenticated in custom CultureProvider

I have to add localization in my web application.

A request is that a grpc service will give the default language for all users, so in the startup I'm trying to read that value but don't know how to use a registered service inside new CustomRequestCultureProvider

public static CultureInfo[] supportedCultures = new[] { new CultureInfo("it-IT"), new CultureInfo("en-US")};

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<ISpaClient, GrpcSpaClient>();

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

    services.Configure<RequestLocalizationOptions>(options =>
        {
            options.DefaultRequestCulture = new RequestCulture(culture: supportedCultures[0].ToString(), uiCulture: supportedCultures[0].ToString());
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;

            options.RequestCultureProviders.Clear();
            options.RequestCultureProviders.Add(new MyCustomRequestCultureProvider());
        });
}

My custom class

public class MyCustomRequestCultureProvider : RequestCultureProvider
{
    public override async Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
    {
        await Task.Yield();

        if (httpContext == null)
        {
            throw new ArgumentNullException(nameof(httpContext));
        }

        if (!httpContext.User.Identity.IsAuthenticated)
        {
            return null;
        }
        
        var culture = httpContext.User
                                 .Claims
                                 .FirstOrDefault(c => c.Type == TipoClaim.linguaPredefinita.ToString())?
                                 .Value;

        if (culture == null)
        {
            return null;
        }

        return new ProviderCultureResult(culture);
    }
}

But here httpContext.User is never Authenticated, even if the page is

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

My best guess is that you called UseRequestLocalization before UseAuthentication.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!