0

After implementing this useful response and moving my Login view to the Shared views folder, it isn't getting initialized when I call it from another controller.

My calling code:

[HttpGet]
public IActionResult AddressCorrectionList()
{
    if (ValidateSecurityToken()) // ensures the app server thinks we're still logged in
    {
        [deleted for clarity]
        return View(model);
    }
    else
    {
        return View("Login", new LoginViewModel { ReturnUrl = "/Report/AddressCorrectionList" });
    }
}

Action in AccountController being called:

[ActionName("Login"), HttpGet]
[AllowAnonymous]
public IActionResult LoginGet(LoginViewModel model)
{
    ViewData["ReturnUrl"] = model.ReturnUrl;  // I set a breakpoint here.

    if (model.ReturnUrl != null)
    {
        model.InfoMessage = "Please login to access " + ResourceNameFor(model.ReturnUrl);
    }

    ModelState.Clear();
    return View(model);
}

If I directly follow a link to the login page, it works fine. My breakpoint in LoginGet is called.

If I try to follow a link to a page that requires a login but I am not logged in, it works fine. I am correctly redirected to the Login page. My breakpoint in LoginGet is called.

The problem arises when I call the View from another controller, such as from the first of the two code snippets. The login page is displayed, however my breakpoint from LoginGet is not called, and my ReturnUrl is not set.

ValidateSecurityToken exists because although the application may consider the user to be logged in, a separate app server to which I make API calls, may log the user out due to its own timeout rules.

How do I fix my call from AddressCorrectionList so my view is properly initialized?

Community
  • 1
  • 1
Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
  • Maybe some clarity would help. So are you calling the shared Login view as a partial view from the parent view? If not, how are you rendering it? – Mike_G Jan 03 '17 at 20:05
  • It's not a partial view. It's just a "normal" `.cshtml` view that used to live in the `Views/Account` directory and is rendered, presumably, within the `RenderBody()` call within `_Layout.cshtml`. (Methinks I'm missing the essence of what you're trying to ask me.) – Bob Kaufman Jan 03 '17 at 20:08

1 Answers1

2

When calling return View("viewName", model) that cshtml file is directly rendered with the given model. The action LoginGet is never hit as you call a method to return a view instead.

What you are looking for is to execute the RedirectToAction("LoginGet") method, or any of it's overloads.

Gigabyte
  • 571
  • 6
  • 14
  • You put me on the right path, and [this answer](http://stackoverflow.com/a/10785274/35142) completed the picture. For my case, the correct use was `RedirectToAction("Login", "Account", model );` – Bob Kaufman Jan 03 '17 at 20:24