Common mistake or misunderstanding why login action doesn’t have ReturnUrl value, even if there is string parameter with value in the page URL. If you think about it more, you will get it – ReturnUrl  is in the page URL but not in the action URL of the login form. The trick is to pass this parameter to the form inputs or action URL parameters be submitted with login form request. It’s very easy to do with MVC.

First, in the view pass parameter to the form by specifying rote parameter for form action:

@model LogOnModel
@{
	ViewBag.Title = "Login | kitsula.com";
	Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>LogOn</h1>
<img src="/Content/Img/login.png" alt="Login" id="imgLogin" />
@using (Html.BeginForm("LogOn", "Account", new { ReturnUrl = Request.QueryString["ReturnUrl"]}, FormMethod.Post, new { @id = "formLogOn"})) { 
	@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
	<div>
		<fieldset>
			<legend></legend>
			<div class="editor-label">
			    @Html.LabelFor(m => m.UserName)
			</div>
			<div class="editor-field">
			    @Html.TextBoxFor(m => m.UserName)
			    @Html.ValidationMessageFor(m => m.UserName)
			</div>
			<br class="clearer" />
			<div class="editor-label">
			    @Html.LabelFor(m => m.Password)
			</div>
			<div class="editor-field">
			    @Html.PasswordFor(m => m.Password)
			    @Html.ValidationMessageFor(m => m.Password)
			</div>
			<br class="clearer" />
			<div class="editor-label"> </div>
			<div class="editor-field">
			    @Html.CheckBoxFor(m => m.RememberMe)
			    @Html.LabelFor(m => m.RememberMe)
			</div>
			<br class="clearer" />
			@Html.HiddenFor(m => m.ReturnUrl)
			<div class="editor-label"> </div>
			<div class="editor-field">
			    <input type="submit" value="Log On"/>
			</div>
		</fieldset>
	</div>
}

ReturnUrl parameter will be passed to the server with form submit request. Now we should handle it on the server. You can declare on action handler separate ReturnUrl parameter or it can be a part of the login model:

public class LogOnModel {

	[Required]
	[StringLength(30)]
	[DisplayName("Username")]
	public string UserName { get; set; }

	[Required]
	[StringLength(50)]
	[DataType(DataType.Password)]
	[DisplayName("Password")]
	public string Password { get; set; }

	[DisplayName("Remember me?")]
	public bool RememberMe { get; set; }

	public string ReturnUrl { get; set; }
}

And action itself:

//
// POST: /Account/LogOn
[HttpPost]
public ActionResult LogOn(LogOnModel model) {

	if (ModelState.IsValid) {
		if (new KitsulaMembershipProvider().ValidateUser(model.UserName, model.Password)) {
			FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
			if (!string.IsNullOrEmpty(model.ReturnUrl))
				return Redirect(model.ReturnUrl);
			return RedirectToAction("Index", "Home");
		}
		ModelState.AddModelError("Password", "The user name or password provided is incorrect.");
	}
 
	// If we got this far, something failed, redisplay form
	return View(model);
}

On successful login just check if ReturnUrl is not empty and redirect to the URL or default action.

Comments

Luis
18 November 2014 | 09:59 -05:00

Thank you very much!! This was exactly what I needed! You rock! : )

Satish
19 January 2016 | 11:03 +05:30

if we need to another window like failure message so hoew to achieve it.

Yevhen
15 May 2019 | 15:59 +03:00
Thank you so much! You saved my time
Casey
01 January 0001 | 00:00 +00:00
It's perfect time to make some plans for the longer term and it is time to be happy. I have learn this put up and if I may just I desire to recommend you some fascinating issues or advice. Perhaps you could write next articles referring to this article. I desire to read even more issues approximately it! Master аnd improve tһе English tongue on the island օf Malta — Ϝorm neԝ relationships... ⭐ English for young learners in Malta — best prices for English courses in Malta, English speaking courses in Malta http://Https%3A%2F%Evolv.E.L.U.Pc@Haedongacademy.org/phpinfo.php?a[]=%3Ca%20href=http://www.mandolinman.it/guestbook/%3EEnglish%20Classes%20For%20Foreigners%20in%20Malta%3C/a%3E%3Cmeta%20http-equiv=refresh%20content=0;url=http://www.mandolinman.it/guestbook/%20/%3E
Lauren
01 January 0001 | 00:00 +00:00
That is a great tip especially to those fresh to the blogosphere. Short but very precise information… Thanks for sharing this one. A must read post! Let's get leads online smoothly... For companies seeking to harvest accurate contact emails from Google Maps, SocLeads.com is invaluable. Its scraping features are strong and effective, much like a tornado sweeping through data. This tool provides high accuracy and efficiency, capturing leads that would otherwise remain hidden online. What distinguishes SocLeads is its user-friendly interface coupled with advanced algorithms focused on extracting reliable data. Whether your goal is client growth or beating competitors, SocLeads produces prompt and effective results. It adapts well into current processes, letting novices handle difficult data without trouble. Mastering SocLeads can elevate your marketing efforts beyond expectations. Employing SocLeads is like executing a strategic masterpiece that pays off big. ▶️ gmap extractor — Gmap scraper free, scraping tools https://noob.comehere.cz/index.php?option=com_phocaguestbook&view=guestbook&id=1&Itemid=109&t=420

Join the discussion

Captcha