dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.43k stars 10.01k forks source link

app.UseRequestLocalization overwritten on deployed IIS #58440

Open Schoof-T opened 2 weeks ago

Schoof-T commented 2 weeks ago

Is there an existing issue for this?

Describe the bug

In Blazor Server, when setting the app.UseRequestLocalization("en-GB"); I was under the assumption that your application should always use the "en-GB" culture.

This works correctly locally in my Visual Studio, I can switch the "en-GB" to any other locale and the whole app will switch. However, I notice that after deploying my application to a local IIS server, the culture is still "en-GB" but for example the decimal seperator is different. (locally the decimal seperator is ".", deployed it's a ",")

It is still using the culture settings of the Windows user that is set as the identity of your application pool.

Image

Expected Behavior

Steps To Reproduce

Exceptions (if any)

No response

.NET Version

8

Anything else?

ASP.NET CORE 8 Blazor Server Visual Studio 2022

cc: @guardrex https://github.com/dotnet/AspNetCore.Docs/issues/33847

Schoof-T commented 2 weeks ago

Investegating further I found that this has to do with the user account running the application pool. Looking in the registry I saw that the user has the Regional Settings of "en-GB", but the decimal seperator is set as ",". Which is different from the default.

I was under the assumption app.UseRequestLocalization("en-GB"); would ignore any user settings. Turns out this is not true. To fully ignore user settings I had to do the following:

    var english = new CultureInfo("en-GB", false);
    var englishRequestCulture = new RequestCulture(culture: english, uiCulture: english);

    var options = new RequestLocalizationOptions
    {
        DefaultRequestCulture = englishRequestCulture,
        SupportedCultures = [english],
        SupportedUICultures = [english]
    };

    app.UseRequestLocalization(options);

Specifically the false parameter of new CultureInfo("en-GB", false); is very important.

Is this expected behaviour and is this a correct solution I have implemented? If it is, It would be nice if this is documented somewhere. Apologies if I missed this in the documentation. :)

guardrex commented 2 weeks ago

You didn't miss it in the documentation AFAIK. This never came up in discussions with the team or with @hishamco.

I'm 👁 on this issue, and I'll work on the guidance using the original docs issue after the product unit responds and lets us know what's going on and how to resolve it.

hishamco commented 2 weeks ago

I'm in the mobile now, I will have a look once I open the laptop

hishamco commented 2 weeks ago

Before I dig into that could you try to set CultureInfoUseUserOverride = false in the RequestLocalizationOptions

Schoof-T commented 2 weeks ago

Before I dig into that could you try to set CultureInfoUseUserOverride = false in the RequestLocalizationOptions

Setting it as follows does not seem work:

    var configuredCulture = "en-GB";
    var supportedUICultures = new[] { configuredCulture };
    var localizationOptions = new RequestLocalizationOptions()
        .SetDefaultCulture(configuredCulture)
        .AddSupportedUICultures(supportedUICultures);
    localizationOptions.CultureInfoUseUserOverride = false;
    app.UseRequestLocalization(localizationOptions);