简体   繁体   中英

How can I set the culture to the user selected culture for the login page?

There are areas of our site that require authentication to access.

Our site has the option to pick the user's preferred language from any page.

For some reason when being redirected to a login page, even after selecting a language, the language is set to the default language on the login page.

For example, if the user is on the index page (anonymous allowed) and selects French as their language, and then selects to go the to DB portal (login required) they are taken to the default language login page (English). After logging in the language is correct.

What should happen is: the user should be redirected to the login page which displays in their selected language.

If I select the language while on the login page, then the login page changes language.

This is the configuration for the login:

<authentication mode="Forms">
    <forms loginUrl="/Home/Login" />
</authentication>

The route config:

routes.MapRoute(
                name: "Localised",
                url: "{language}/{controller}/{action}/{id}",
                constraints: new {language = @"(\w{2})|(\w{2}-\w{2})" },
                defaults: new {language = "en-GB", controller = "Home", action = "Index", id = UrlParameter.Optional }
               );

routes.MapRoute(
                name: "login",
                url: "Home/Login",
                defaults: new 
                          { 
                              language = CultureInfo.CurrentCulture.TwoLetterISOLanguageName,
                              controller = "Home", 
                              action = "Login"
                          }
               );

I'm using this method to do the localisation.

For some reason, even though the language is set previously, the current culture is the default culture.

How should I configure the route to use the previously selected culture?

My current solution is to set a session variable in the LocalizedControllerActivator.Create method, to store the culture for the session, and then in set the Current Culture in the handler for the Login request:

public IController Create(RequestContext requestContext, Type controllerType)
{
    string lang = requestContext.RouteData.Values["language"] as string ?? _DefaultLanguage;

    if (lang != _DefaultLanguage)
    {
        try
        {
            Thread.CurrentThread.CurrentCulture =
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
            // Store the culture in the session here
            requestContext.HttpContext.Session["culture"] = Thread.CurrentThread.CurrentCulture;
        }
        catch (Exception)
        {
            throw new NotSupportedException(string.Format("ERROR: Invalid language code '{0}'.", lang));
        }
    }

    return DependencyResolver.Current.GetService(controllerType) as IController;
}

public ActionResult Login(UserLogin user = null, string ReturnUrl = "/")
{
    Thread.CurrentThread.CurrentCulture =
    Thread.CurrentThread.CurrentUICulture = Session["culture"] as CultureInfo;

    //do the rest of the login stuff below
    // ...
    return View();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM