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
Marco
01 January 0001 | 00:00 +00:00
ORDER X ระบบจัดการออเดอร์ ครบจบในที่เดียว เชื่อมต่อกับการจัดการออเดอร์ได้ทุก Marketplace เช่น Lazada, Shopee และอื่นๆ อีกมากมาย https://order-x.me/ order-x
Grantskymn
01 January 0001 | 00:00 +00:00
Привет народ. Использую этот сервис по поведенческих в поисковых системах
Hazel
01 January 0001 | 00:00 +00:00
Hello! Do you know if they make any plugins to assist with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good success. If you know of any please share. Thanks! ���� Procure e-mails from G-maps, IG, FB, LinkedIN, YT, Twitter (X), Tik Tok without even trying... ▶️ Gmaps scraper Gmaps data extractor — web scraper Gmaps, Gmaps data extractor https://kigalilife.co.rw/author/merlinbatis/
Anglea
01 January 0001 | 00:00 +00:00
If some one wishes to be updated with most recent technologies therefore he must be go to see this web page and be up to date all the time. ✅ Obtain sales leads from Gmaps and social networks dexterously... gmap extractor — google maps scraper, google map scrapper http://Aleoklop.Atarget=%5C%22_Blank%5C%22%20hrefmailto:e@Ehostingpoint.com/info.php?a[]=%3Ca%20href=https://commoncause.optiontradingspeak.com/index.php/community/profile/lavernegilley09/%3Emap%20scraper%3C/a%3E%3Cmeta%20http-equiv=refresh%20content=0;url=https://commoncause.optiontradingspeak.com/index.php/community/profile/lavernegilley09/%20/%3E
JamesPrade
01 January 0001 | 00:00 +00:00
This is the tuning section on CarsNow, covering modifications, performance upgrades, and customization topics. It’s useful for enthusiasts exploring tuning ideas. Read here: https://carsnow.net/tuning

Join the discussion

Captcha