asp.net-core-mvcasp.net-core-localization

ASP.Net Core CookieRequestCultureProvider not working


I have an ASP.Net Core 3.1 app with the following startup.cs (I have tried various combinations of the below configuration based on web searches):

public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.Configure<RequestLocalizationOptions>(options =>
        {
            options.RequestCultureProviders = new[] { new CookieRequestCultureProvider() };
        });

and

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            ...
            var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
            app.UseRequestLocalization(options.Value);

And in my app's logon method, I'm setting the Culture Cookie as follows:

HttpContext.Response.Cookies.Append(
                            CookieRequestCultureProvider.DefaultCookieName,
                            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture([logged-in-user].CultureCode)));

In subsequent requests I can see the cookie in my browser dev tools Network tab: cookie seen in browser dev tools

Yet, on the server, any given request still maintains the default server culture (which is en-ZA) for my dev environment. (I'm seeing this by checking System.Threading.Thread.CurrentThread.CurrentCulture.Name in any breakpoint in my server action methods)

And I'm running into date conversion issues between my client and my server (e,g client with en-US culture as per screenshot above) sends a date of 3/5/2009 (March 5th) to the server, and the server is interpreting it as May 3rd.

Why is my server not honoring the CultureCookie? What am I missing?


Solution

  • As you mentioned, you have registered your localization service in your ConfigureServices method.

    My suggested way is to use it like:

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

    But don't forget to register the middleware:

    // In StartUp.cs Configure method
    var SupportedCultures = new CultureInfo[]
    {
        new CultureInfo("en"),
        new CultureInfo("zh")
    };
    app.UseRequestLocalization(new RequestLocalizationOptions
    {
        DefaultRequestCulture = new RequestCulture(defaultLanguage),
        SupportedCultures = SupportedCultures,
        SupportedUICultures = SupportedCultures
    });
    

    As for your cookie end-time issue, please try to specify the end date of your cookie. Like this:

    Response.Cookies.Append(
        CookieRequestCultureProvider.DefaultCookieName,
        CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
                        new CookieOptions
                        {
                            Expires = DateTimeOffset.UtcNow.AddYears(1),
                            SameSite = SameSiteMode.None
                        });