1
votes

I have an MVC application created from the Internet template to use forms authentication, when the user has logged on I want to direct them, say, to the Home|About page.

The LogOn method is that created by the project template, without modification;

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
           // etc, etc
        }

        return View(model);
    }

I've set the defaultUrl in Web.config as follows;

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" defaultUrl="~/Home/About"/>
</authentication>

However, when HomeController.LogOn is called the returnUrl parameter is always null. Note the HttpPost attribute on the LogOn method, so the url cannot be passed in the query string.

How can I configure a return url so it gets passed to the LogOn action method and the user is directed to the url's location after log on?

1
did you tried return RedirectToAction("Logon"); ? - Yusubov
Most probably you forgot to post your reurnUrl string in Logon view ? For this purpose, you may use @Html.HiddenFor() helper as well. - Yusubov

1 Answers

1
votes

Easiest way is to change the default route in the Global.asax to point to /Home/About instead of /Home/Index

public static void RegisterRoutes(RouteCollection routes) {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "About", id = UrlParameter.Optional } // Parameter defaults
    );
}