Advanced custom multilingual url handling / Role & membership LoginUrl

334 Views Asked by At

I'm working on a custom FriendlyURL analyzer for my cms (myprettycms.codeplex.com). The system is multilingual:

If you look in the file :

https://myprettycms.codeplex.com/SourceControl/changeset/view/19148#457568

it's the base class of 2 controllers. One is public and the other private. The difference between 2 code is Authorize decoration in the private one.

The login url depends on the language of the page you are trying to access.

If I'm on an English page like:

http://www.mydomain.com/en-us/products/priduct_list.htm

and if this page is private (the controller is set to DynaContentPrivateController) and if I'm not authenticated, the system will redirect my demand to the LoginURL defined in the web.config.

At this time, it's set to "~/logon"

I would have something like "~/{0}/logon" and know in witch part of the code I can override a code to replace {0} with the language of the page I was looking for.

Could you help me ?

1

There are 1 best solutions below

0
To delete profile On BEST ANSWER

I found the solution...

I kept the web.config setting :

<authentication mode="Forms">
  <forms loginUrl="~/LogOn" timeout="2880" />
</authentication>

Web.Config in codeplex

In the RouteConfig.cs

routes.MapRoute(
    name: "LogOn",
    url: "LogOn/{action}",
    defaults: new { controller = "LogOn", action = "SelectLanguage" }
);

And in the languages routes loop in RouteConfig.cs

foreach (var l in Languages)
{
    ...
    routes.MapRoute(
    name: "LogOn" + l.Name.ToUpper(),
    url: l.Description + "/logon/{action}",
    defaults: new { controller = "LogOn", action = "Index", language = l.Description }
    );
    ...
}

The routeconfig.cs in codeplex

In the controller base

public string URLReferLanguage
{
    get
    {
        string toReturn = ConfigurationManager.AppSettings["DefaultLanguage"];
        string language = string.Empty;
        string[] tLanguages = ConfigurationManager.AppSettings["Languages"].Split(',');
        string urlReferrer = HttpContext.Request.UrlReferrer.AbsolutePath;
        if (urlReferrer != null)
        {
            if (urlReferrer.Length > 5)
            {
                language = urlReferrer.Substring(1, 5).ToLower();
                if (tLanguages.Contains(language))
                {
                    toReturn = language;
                }
            }
        }
        return toReturn;
    }
}

_Controller_Base in codeplex

In the LogOn Controller in Security Layer

public ActionResult SelectLanguage()
{
    return Redirect(string.Format("/{0}{1}", base.URLReferLanguage, Request.Url.PathAndQuery));
}

/// <summary>
/// URL: /en-us/LogOn
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
    string eMethod = eMethodBase + ".Index[GET]";
    using (DataRepositories _dataContext = new DataRepositories())
    {
        base.InitView(
            Resources.View_LogOn_PageTitle,
            string.Empty,
            specificCssCollection,
            specificJSCollection,
            dynamicJSCollection,
            specificJqueryJSCollection,
            jsVariables,
            externalCSS,
            Meta,
            _dataContext,
            false,
            base.RequestLanguage,
            false
        );
    }
    string returnUrl = Request.QueryString["ReturnUrl"];
    if (!string.IsNullOrEmpty(returnUrl))
    {
        ContentData.ReturnUrl = returnUrl;
    }
    return View(ContentData);
}

The LogOn controller in codeplex

So, when a user is redirected to LoginForm by standard role and membership (my Provider inherit standard one), controller analyse UrlRefer and add language to address and redirect to it.