Click here to Skip to main content
14,738,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm working on an MVC app that has login authentication and times out after a while of being logging in. So I want to be able to log in and go back to the page I was rather than going to a static home page.


Every time I get kicked out, I do get a "Account/Login?ReturnUrl = ... " on my browser, but my debugger inside Login ActionResult post method always says my returnUrl is null. I tired the following method, before this I triedo other methods too but none have worked. So I'm really stuck please help:

What I have tried:

"AccountController.cs" :

public sealed class AccountController : BaseController
    {
        //Login prompt
        // GET: /Account/Login
        [AllowAnonymous]
        public ActionResult Login() {

            return View();

        }

        //Login processing(authentication)
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        
        public ActionResult Login( LoginViewModel model, string returnUrl) {
            if ( ModelState.IsValid ) {
                var authenticated = AuthenticationProvider.Authenticate( model.UserName, model.Password, HttpContext );
                
                if( authenticated ) {
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        ViewBag.ReturnUrl = returnUrl;
                        return Redirect(returnUrl);
                    }
                  
                }

                ModelState.AddModelError( "", "Invalid username or password." );
            }

            return View( model );
        }



"Loging.cshtml" form:
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { ReturnUrl = Request.QueryString["ReturnUrl"] }))
    { .... 


"web.config":

<modules>
     <remove name="FormsAuthentication" />
     <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
   </modules>
Posted
Updated 1-Mar-19 8:11am
Comments
MadMyche 1-Mar-19 12:50pm
   
How long is "after a while of being logging in"? Are you going through the site or staring at a page for that time?

1 solution

You're going nothing with the ReturnUrl param in the HttpGet. In order for that to be available in the HttpPost you'll need to store it in the form.

Amend the LoginViewModel class to add a ReturnUrl property

public string ReturnUrl { get; set; }


Capture the url in the HttpGet method and ensure it is included in the form

public ActionResult Login(string returnUrl)
{
    LoginViewModel model = new LoginViewModel();
    model.ReturnUrl = returnUrl;

    return View(model);
}

[HttpPost]
public ActionResult Login(LoginViewModel model)
{
    return View(model);
}


@model LoginViewModel

@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.ReturnUrl)

    // rest of form

    <input type="submit" value="Login"/>
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900