Home > Software engineering >  .NET6 app ignores Accept-Language and always uses Regional settings of IIS ApplicationPoolIdentity u
.NET6 app ignores Accept-Language and always uses Regional settings of IIS ApplicationPoolIdentity u

Time:05-23

I am struggling to get working Localization (only decimal separators and calendar format - not language specific strings) in my .NET6 Razor app based on currently logged in user, app is using Windows authentication and is deployed on IIS.

In my startup.cs I have

var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(options.Value);

And I can see supported Cultures while debugging on my local machine, which are my local Regional settings not related to locales sent in request via Accept-Language enter image description here

enter image description here

In app for @System.Globalization.CultureInfo.CurrentCulture/@System.Globalization.CultureInfo.CurrentUICulture

enter image description here

But Accepted-Language is only en-US

Accept-Language: en-US,en;q=0.9,cs;q=0.8

Same thing happens when app is deployed to IIS Application pool. System user which is set in Identity has it's own Regional settings and these are used for all incoming requests, and Accept-Language is ignored.

enter image description here

Do you have any suggestion how I could format numbers, dates,... based on client's Accept-Language and their specific regional settings?

Edit: Following suggested link from @Rena tried to play with it more, but without success, client supported Formats are ignored, and it is using only Formatting available for Identity user on IIS. Here is screenshot from IIS, please note that "Welcome screen" settings are applied to system accounts as well.

enter image description here

Now if I visit my site hosted on this IIS I will get formatting en-GB enter image description here even tho I have locale on client computer set to Czech.

enter image description here

CodePudding user response:

Ok, so this was just mine misunderstood how Globalization works. In order to not use set locale on IIS app needs to have defined Supported Cultures in Startup.cs

    var supportedCultures = new[] { 
        new CultureInfo("cs-CZ"), 
        new CultureInfo("fi-FI"), 
        new CultureInfo("de-DE"), 
        new CultureInfo("ro-RO"), 
        new CultureInfo("pl-PL") 
    };
    app.UseRequestLocalization(new RequestLocalizationOptions
    {
        DefaultRequestCulture = new RequestCulture("cs-CZ"),
        SupportedCultures = supportedCultures,
    });

Then if user's locale is found in Supported Locales this locale is used, otherwise app falls back to set IIS locale.

  • Related