Log off and redirect user to login screen ASP NET MVC / C #

1

I'm looking for a way to end my session and redirect the user to the login screen when my system TimeOut .

I tried to use Session.Abandon () according to some examples that I researched. But I do not know what I'm doing wrong. below is my code:

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        var context = new HttpContextWrapper(Context);

        if (context.Response.StatusCode == 302 && context.Request.IsAjaxRequest())
        {
            var redirectLocation = context.Response.RedirectLocation.ToString();

            context.Response.RedirectLocation = null;
            context.Response.ContentType = "text/plain";
            context.Response.Write("session_timeout;" + redirectLocation);

            context.Session.Abandon();
            context.Response.Redirect("~/Account/Login");
        }
    }

The code runs only until: context.Session.Abandon (); and does not redirect to the login screen unless I give refresh on the page.

    
asked by anonymous 18.01.2018 / 11:57

1 answer

1

You can use:

System.Web.HttpContext.Current.Session.RemoveAll();            
System.Web.Security.FormsAuthentication.SignOut();

I can not remember now if this code snippet already redirects to the login screen, but if you do not, you can use your redirect:

context.Response.Redirect("~/Account/Login");

EDIT

Then try changing your method from void to ActionResult and return RedirectToAction() :

//Mesmo Código Acima...
return RedirectToAction("Login", "Account");

Using your View and Controller Login

    
18.01.2018 / 14:10