2

FYI - This is not MVC.

I am using web form authentication and have the following in my web.config.

<authentication mode="Forms">
  <forms loginUrl="~/en/Admin/Login" timeout="2880" defaultUrl="/DashBoard" />
</authentication>

I am also using Routing for bilingual/culture.

My route looks like this:

RouteTable.Routes.MapPageRoute(
    routeName, "{lang}/Admin/Login", "/Admin/Login.aspx", true, defaults, constraints, dataTokens);

If a user tries to access a restricted page they will be redirected to /en/Admin/Login based the value in the web.config. My problem is if a user is viewing the site in french, the page is redirected to the English log in page when it needs to redirect to /fr/Admin/Login.

Is there any way around this as the entire site needs to be bilingual?

PsychoDUCK
  • 1,073
  • 3
  • 17
  • 34
  • I found a similar issue with a few work arounds, but no true solution. http://stackoverflow.com/questions/356982/how-to-redirect-to-a-dynamic-login-url-in-asp-net-mvc – PsychoDUCK Apr 19 '12 at 20:02

2 Answers2

0

On the default (en//admin/login) page, parse the referring URL and redirect as needed. (Assuming locale is determined on your site by the culture marker in the URL path.) Eg,

var referrer = Request.QueryString["ReturnUrl"];
if (!string.IsNullOrEmpty(referrer))
{
    if (!referrer.Contains("/Admin/Login"))
    {
        if (referrer.Contains("/fr/")) Response.Redirect("/fr/Admin/Login");
        else if (referrer.Contains("/de/")) Response.Redirect("/de/Admin/Login");
        // etc.
    }
}

Of course that could be improved/simplified by using a regular expression to parse the referrer and checking against valid locales.

McGarnagle
  • 96,448
  • 30
  • 213
  • 255
  • Request.UrlReferrer is always null. I'm not sure that this is the best solution. Seems like a hack. – PsychoDUCK Apr 19 '12 at 19:46
  • @PsychoDUCK hmm, interesting. Makes sense though I guess, since the original auth redirect is internal to the application. I think you could still use the same approach by using the RedirectUrl in the query string, instead of the URL referrer. Admittedly kind of a hack, but it seems like the best solution given your setup. – McGarnagle Apr 19 '12 at 19:50
  • I'm open to suggestions, on a better approach. – PsychoDUCK Apr 19 '12 at 19:52
0

I found a similar issue with a few work arounds, but no true solution. How to redirect to a dynamic login URL in ASP.NET MVC

Here's my solution:

1) I added a session variable to keep track of what language the user has selected. (Ex: Session["lang"] = "fr")

2) I made my login page /admin/default.aspx in the web.config like below:

<authentication mode="Forms">
  <forms loginUrl="~/Admin/Default.aspx" timeout="2880" defaultUrl="/en/DashBoard" />
</authentication>

3) In my page load event for /admin/default.aspx I determine what language is set and redirect to the actual login page using the language from the session.

    if (HttpContext.Current.User.Identity.IsAuthenticated)
        // Redirect to dashboard
        ...
    else
    {
        string returnUrl = "";
        if (Request.QueryString["ReturnUrl"] != null)
            returnUrl = "?ReturnUrl=" + Request.QueryString["returnUrl"].ToString();

        string selectedLanguage = "";
        if (Session["lang"] != null)
            selectedLanguage = Session["lang"].ToString();
        else
            selectedLanguage = "en";

        string loginURL = ConfigurationManager.AppSettings["Auth.LoginUrl"].ToString();
        loginURL = loginURL.Replace("{lang}", selectedLanguage);

        Response.Redirect(loginURL + returnUrl);                
    }
Community
  • 1
  • 1
PsychoDUCK
  • 1,073
  • 3
  • 17
  • 34