How to force login after running the application?

4

My scenario is as follows:

After authentication with Active Directory, the logged-in user is saved in a Session

[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);
}

After that I make some changes to the code and run the application again, but the Session does not stay there and the application thinks the user is still logged in.

How can I clean up this thing that makes the user still look online?

    
asked by anonymous 21.10.2014 / 21:14

1 answer

3

Make the user quit the session in the Application_End event of your Global.asax.cs :

void Application_End(object sender, EventArgs e) 
{
    Session.Abandon();
}

PS: I have not tested this code.

EDIT

Since the above code did not work, I think the programmatic way might not be a good way out. In the Web.config file, modify the following:

<authentication mode="Forms">
    <forms cookieless="UseCookies" loginUrl="~/Account/Login" name="MinhaAutenticacao" slidingExpiration="true" timeout="300"/>
</authentication>
  • timeout confers that the Cookie will last at most 5 minutes if the user is idle (ie without making any requests to the server);
  • slidingExpiration Renews Cookie Expiration if any request is made within five minutes.
21.10.2014 / 22:03