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?

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Request.UrlReferrer is always null. I'm not sure that this is the best solution. Seems like a hack.
@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.
I'm open to suggestions, on a better approach.
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);                
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.