There are a few pieces to the puzzle. When your LogOn action is called the first time the Forms Authentication mechanism will have added a query string parameter called returnUrl. You need to capture this. Something like
public ActionResult LogOn(string returnUrl)
{
ViewData["ReturnUrl"] = returnUrl;
return View();
}
Your view needs to have a hidden field so that the returnUrl can be passed back to the authentication action. Somewhere inside the form which collects username and password (in the case above it would be called LogOn.aspx) you will want
<%= Html.Hidden("returnUrl", ViewData["ReturnUrl"]) %>
Assuming the the LogOn form submits to an action called Authenticate your code will be
public ActionResult Authenticate(string username, string password,
string retuUrl)
{
if (ValidLogin(username, password) {
FormsAuthentication.SetAuthCookie(username, false);
if (returnUrl == null)
return RedirectToAction("someAction", "someController");
else
return Redirect(returnUrl);
}
else
{
return RedirectToAction("LogOn", new {returnUrl = returnUrl});
}
} There are probably neater ways of doing this, but it works for me.