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?