Close session in mvc c #?

0

I have the following code for a log

Response.Cookies["correoRepresentante"].Value = CorreoL;
Response.Cookies["correoRepresentante"].Expires = DateTime.Now.AddYears(1);
Response.Cookies["passRepresentante"].Value = contrasena;
Response.Cookies["passRepresentante"].Expires = DateTime.Now.AddYears(1);

and for the logout:

var c = new HttpCookie("correoRepresentante");
c.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(c);

var p = new HttpCookie("passRepresentante");
p.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(p);
Response.Cookies.Clear();
Session.Clear();
return View("log");

but then I put anything back in the log and enter the session I had.

    
asked by Alcides Salazar 30.11.2016 в 01:49
source

3 answers

0

For what you say, I understand that the session is releasing it well and that the problem may be in the validation that you do with the data that comes in the session variable. In any case I recommend you also use Cookies.Remove(Nombrecookie) and Session.Abandon() .

I hope it serves you.

    
answered by 30.11.2016 / 11:22
source
1

If you use FormsAuthentication I recommend you use:

public ActionResult LogOut()
{
    FormsAuthentication.SignOut();
    Session.Abandon();
    return RedirectToAction("index", "login");
}

If you use OWIN Authentication I recommend using just one of the following:

Request.GetOwinContext().Authentication.SignOut();

Request.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

HttpContext.Current.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);
    
answered by 30.11.2016 в 16:43
0

Try changing this line:

c.Expires = DateTime.Now.AddDays(-1);

by:

c.Expires = DateTime.Now.AddYears(-1);

When you create the cookie you are adding 1 year to expire and at the time of closing the session you are only removing one day.

    
answered by 27.09.2018 в 17:35