views:

31

answers:

2

Hi there,

I hope someone can help with solution to this problem?

Currently my ASP.Net MVC website uses forms authentication is set up like this my web.config:

<authentication mode="Forms">
  <forms loginUrl="en/User/Signin" timeout="2880" />
</authentication>

We have some routing rules that use the prefix /en/ in the url as a identifier for the language, but the problem is that if someone is visiting our french site www.web.com/fr/Secure/privateData, they are redirect to www.web.com/en/User/Signin, which in turn sets the culture to english. So after logging in, users may need to change there language back to french.

Not good!

So if the website need to suppurt more languages, so I need to do something like this in the web config:

<authentication mode="Forms">
    <%if (isGerman()) { %>
        <forms loginUrl="de/User/Signin" timeout="2880" />
    <%} else if (isFrench()) {%>
        <forms loginUrl="fr/User/Signin" timeout="2880" />
    <%} else { %>
        <forms loginUrl="en/User/Signin" timeout="2880" />
    <% } %>
</authentication>

I know you can not have code in the web.config, but this is just to illustrate what I am trying to achieve. Could anyone provide a simple solution, or links to solutions they may already use?

Thanks alot!

+1  A: 

Can't you just globalize one login page and show the language strings depending on the language setting?

Otherwise you should use some kind of redirect, set one login page and redirect to the correct page depending on the language settings within that page. However you might need to add extra code to your config telling the system that the language versions of the login are not protected within the authentication mode (you can exclude/change settings for certain parts of the site in the web.config) to prevent endless looping.

Gertjan
thanks, there are a couple of solutions we have thought of, put language in cookie or sessions, a number of "hacks" I can think of, but is there a proper way of doing it with forms authentication?
Dai Bok
I don't think you will be able to let the authentication handle the globalization. The authentication part is only responsible for looking whether someone is authenticated or not and handle those situations. That process is not aware of any globalization.I think you really must put the globalization into the page used for logging in.
Gertjan
Thanks Gertjan, I think we will probably do something like this.
Dai Bok
+1  A: 

Dai,

I do something very similar and have a basecontroller that sets the language on correct login. I dont have the code to hand right now but it's something along the lines of:

    public string Lang { get; private set; } // at the top of the abstact basecontroller

    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        Lang = requestContext.RouteData.Values["lang"].ToString() ?? System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
        ViewData["Lang"] = Lang;

        base.Initialize(requestContext);
    }

I then plug the Lang variable into the routes as required. I'll see if i can dig out the code that i use but hopefully the above will be enough to get your head around it.

jim

jim
Yip, been thinking about "hacks" around it, like keeping the language code in tmpdata, or something similar. I was hoping there would be some object property you could manipulate like:controller.httpContext.formsAuthenticationObject.loginurl?
Dai Bok