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

Asked

Viewed 1,544 times

1

I am looking for a way to terminate my session and redirect the user to the login screen when my system der Timeout.

I tried to use Session.Abandon() according to some examples I researched. But I don’t 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 is executed only until : context.session.Abandon(); and does not redirect to the login screen unless I give refresh on the page.

  • I don’t know in Asp.net MVC, but in Webforms there is a file called Global.asax that every time the session inspires the method void Application_End(object sender, EventArgs e) is called.

  • @Marconi Gloabal.asax also exists in Asp net mvc, this code is inside it, but it still doesn’t work. Thanks

  • Places a break point within the method.

  • I put the break point, but it doesn’t come to that point context.Response.Redirect("~/Account/Login");

  • Put on if to see if the conditions are meeting.

1 answer

1

You can use:

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

I can’t remember in my head now if this snippet of code already redirects to the login screen, but if it doesn’t you can use its redirect:

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

EDIT

Then try to change the type of your Method of void for ActionResult and return a RedirectToAction():

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

Using your View and Login Controller

  • Thank you for your reply, but does not redirect to the page even using. context.Response.Redirect("~/Account/Login");

  • I changed the answer to another Alternative, try again! D

  • All you need to do is re-turn Redirecttoaction("Login", "Account");

  • Truth @Gustavocorreintos to in the rush and I didn’t even care! Thanks, I’ve edited

  • Thanks for the answer, that employee. But I get a question. If I implement this solution using my Login Controller, could I check if a user had timeout? What I mean is... for an already logged in user, can I check if they gave timeout and redirect to the login screen? i believe not, because I would need to call my Actionresult every time from any part of my system. Note: I don’t know

  • I removed the context.Session.Abandon(); is the redirect worked... however, it calls the Login view on top of the current view, thus breaking my screen...

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.