Here is my code. Please some one help me desperately needed the code above mentioned.
[HttpGet]
public ActionResult Index(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
[HttpPost]
public ActionResult Index(LoginModel loginModel, string returnUrl)
{
if (ModelState.IsValid)
{
if (loginModel.Username == "user" && loginModel.Password == "password")
{
FormsAuthentication.SetAuthCookie(loginModel.Username, true);
return Redirect(returnUrl);
}
else
{
ModelState.AddModelError("", "The username or password provided is incorrect.");
}
}
ViewBag.ReturnUrl = returnUrl;
return View(loginModel);
}
And I am following this link: http://www.primaryobjects.com/CMS/Article155.aspx
Where is the problem:
Aaand what happen if you hit your action without returnUrl parameter and you pass null to the Redirect() method ?? - You get exactly this error :).
Solution:
You can check if the url is not null or use the RedirectToLocal method which microsft has included in the default mvc template (or write your own or .. etc just don't pass null to the Redirect method):
...
FormsAuthentication.SetAuthCookie(loginModel.Username, true);
// Here 'return Redirect(returnUrl);' become:
return RedirectToLocal(returnUrl);
...
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Home");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With