سؤال

I found this code to catch when my session expires:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SuburbanCustPortal.MiscClasses
{
    public class SessionExpireFilterAttribute : ActionFilterAttribute {

        public override void OnActionExecuting( ActionExecutingContext filterContext ) {
            HttpContext ctx = HttpContext.Current;

            // check if session is supported
            if ( ctx.Session != null ) {

                // check if a new session id was generated
                if ( ctx.Session.IsNewSession ) {

                    // If it says it is a new session, but an existing cookie exists, then it must
                    // have timed out
                    string sessionCookie = ctx.Request.Headers[ "Cookie" ];
                    if ( ( null != sessionCookie ) && ( sessionCookie.IndexOf ( "ASP.NET_SessionId" ) >= 0 ) ) 
                    {
                      ctx.Response.Redirect("~/Home/Login");
                    }
                }
            }

            base.OnActionExecuting ( filterContext );
        }
    }
}

It is firing without issue but the called action is still running it's course:

[Authorize]
[SessionExpireFilter]
public ActionResult PrePayment(PaymentModel.PrePayment model)
{
  if (string.IsNullOrWhiteSpace(Session[SessionEnums.CurrentAccountGuid.ToString()].ToString()))
  {
    return RedirectToAction("AddCustomer", "Customer");
  }

  if (TempData["ViewData"] != null)
  {
    ViewData = (ViewDataDictionary) TempData["ViewData"];
  }

...

[SessionExpireFilter] is running on this code but then it continues on with the rest of the method which causes issues because session is no longer valid. I get an error on the first line of PrePayment since it is returning.

Shouldn't the redirect prevent this from happening?

EDIT **

I made the changes suggested and now have:

namespace SuburbanCustPortal.MiscClasses { public class SessionExpireFilterAttribute : ActionFilterAttribute {

    public override void OnActionExecuting( ActionExecutingContext filterContext ) 
    {
        HttpContext ctx = HttpContext.Current;

        // check if session is supported
        if ( ctx.Session != null ) {

            // check if a new session id was generated
            if ( ctx.Session.IsNewSession ) {

                // If it says it is a new session, but an existing cookie exists, then it must
                // have timed out
                string sessionCookie = ctx.Request.Headers[ "Cookie" ];
                if ( ( null != sessionCookie ) && ( sessionCookie.IndexOf ( "ASP.NET_SessionId" ) >= 0 ) ) 
                {
                  //ctx.Response.Redirect("~/Account/LogOn/");
                  filterContext.Result = new RedirectResult("~/Account/LogOn");
                  return;
                }
            }
        }

        base.OnActionExecuting ( filterContext );
    }
}

}

Prepayment is being called by AccountScreen and it seems that it is being reloaded after the return.

هل كانت مفيدة؟

المحلول

Use this:

   filterContext.Result = new RedirectResult("~/Home/Login");
   return;

Instead of:

ctx.Response.Redirect("~/Home/Login");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top