nam
nam

Reputation: 23859

What is the use of ViewData["ReturnUrl"] = returnUrl in action methods of a controller

When I create an ASP.NET Core Web project using Individual User Accounts authentication, I notice that VS2015 creates an AccountController with lots of action methods. Most of these action methods contain input optional parameter string returnUrl = null and inside these methods we've ViewData["ReturnUrl"] = returnUrl; as shown in one example below.

Question:

  1. What is the use of this optional input parameter and the statement ViewData["ReturnUrl"] = returnUrl; inside these action methods? 2. When should I use these in the action methods and when I should avoid using them?

Get Method:

[HttpGet]
[AllowAnonymous]
public IActionResult Login(string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;
    return View();
}

Post Method:

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);
                if (result.Succeeded)
                {
                    _logger.LogInformation(1, "User logged in.");
                    return RedirectToLocal(returnUrl);
                }
                if (result.RequiresTwoFactor)
                {
                    return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning(2, "User account locked out.");
                    return View("Lockout");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return View(model);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

Upvotes: 3

Views: 9141

Answers (3)

Victor.Uduak
Victor.Uduak

Reputation: 2824

You can parse the returnUrl on the view like this

<form asp-controller="Account" asp-action="Login" asp-route-returnurl="@viewdata["ReturnUrl"]" method="post" class="form-horizontal">

.....//rest of the html removed for brevity

Solution was found Here

Upvotes: 5

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

When you go to a website that requires authorization, you need to return the user back to the page they were originally going to after they have authenticated. This saves that return url so that it can be used to redirect the user back to their original page (rather than a default page like a home page). If you want the user to always go to a default page after login, then you can remove this. You'll need to put the default page redirection in place though.

Upvotes: 4

Norbert Huurnink
Norbert Huurnink

Reputation: 1316

You need to pass the return url to your view when you want to return the user to the page he originally wanted to go to, before he got prompted to login.

You can do this using ViewData, or as a regular property of your model.

So you pass the returnurl around, simply because you don't want to "forget" where the user came from.

Upvotes: 9

Related Questions