How to maintain a Session after re-executing an application?

1

In my project in MVC 5, a session with user data is created at login and after re-execution of the Session application no longer exists, however, the Coockie authentication still exists.

What can I do to keep both active or destroy both?

[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (!this.ModelState.IsValid)
        return View(model);

    if (Membership.ValidateUser(model.Usuario, model.Senha))
    {
        Session.Add("Usuario", new UsuarioModel { Nome = "Eu", Login = "Filipe"});
        FormsAuthentication.SetAuthCookie(model.Usuario, false);
        if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\"))
        {
            return this.Redirect(returnUrl);
        }

        return this.RedirectToAction("Index", "Home");
    }

    this.ModelState.AddModelError(string.Empty, "O usuário ou a senha são inválidos");

    return View(model);
}
    
asked by anonymous 24.10.2014 / 14:44

1 answer

2

By default the storage mode of the session is In-Proc , that is, the information is stored in server memory, however, if the application restarts its session information is lost.

An alternative to keeping your session data even after restarting your application is to change the storage mode of the session.

You can use the SQLServer option that stores session information in the database, another option is StateServer strong> that stores session information in a separate process on the server, isolating session state from your application.

To change the available modes, simply add the <sessionState> tag inside the <system.web> tag in the Web.Config file and configure it as needed.

Follow these links for more details: Link1 Link2 Link3

    
24.10.2014 / 17:43