Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session variables in action filter

I have an action filter checks whether or not a session variable ID is set. For development purposes, I have manually set this variable prior to the check.

public class MyActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext context)
        {

            context.HttpContext.Session.Add("ID", 123123);

            int ID = (int)context.HttpContext.Session.Contents["ID"];
            var rd = context.HttpContext.Request.RequestContext.RouteData;

            TED _db = new TED();

            //if not in DB
            if (_db.Users.Find(ID) == null && rd.GetRequiredString("action") != "NoAccess")
            {
                RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
                redirectTargetDictionary.Add("action", "NoAccess");
                redirectTargetDictionary.Add("controller", "Home");
                redirectTargetDictionary.Add("area", "");

                context.Result = new RedirectToRouteResult(redirectTargetDictionary);
            }

            base.OnActionExecuted(context);
        }
    }

As far as I understand, this code is run prior to any page, this Session["ID"] is always set. The site works fine if I am consistently testing, but it seems to break if I leave it for a while then try to continue testing. Here is the error I get:

int UserID = (int)Session.Contents["ID"];
System.NullReferenceException: Object reference not set to an instance of an object.

Initially I thought the session may simply be expiring, but prior to any page loading, Session["ID"] should be getting set. What is the issue here?

like image 229
Jeff Avatar asked Feb 06 '13 17:02

Jeff


People also ask

What are session variables?

A session variable is a special type of variable whose value is maintained across subsequent web pages. With session variables, user-specific data can be preserved from page to page delivering customized content as the user interacts with the web application.

What is session variable in MVC?

Sessions values can be stored for the duration of the visitor's session on your site. Most cases, they are stored in server memory. You can configure session to store either in State server or in SQL Server. In ASP.NET MVC, you can create and access session variables using HttpContext.

What are session variables in C#?

The Session object stores information about, or change settings for a user session. Variables are stored in a Session object hold information about one single user. And are available to all pages in one application. Common information stored in session variables are name, id, and preferences.


1 Answers

you're implementing your actionfilter's on OnActionExecuted method which executes AFTER your action method

You should implement the OnActionExecuting method

public class MyActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {

            context.HttpContext.Session.Add("ID", 123123);

            int ID = (int)context.HttpContext.Session.Contents["ID"];
            var rd = context.HttpContext.Request.RequestContext.RouteData;

            TED _db = new TED();

            //if not in DB
            if (_db.Users.Find(ID) == null && rd.GetRequiredString("action") != "NoAccess")
            {
                RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
                redirectTargetDictionary.Add("action", "NoAccess");
                redirectTargetDictionary.Add("controller", "Home");
                redirectTargetDictionary.Add("area", "");

                context.Result = new RedirectToRouteResult(redirectTargetDictionary);
            }

            base.OnActionExecuting(context);
        }
    }
like image 81
Dave Alperovich Avatar answered Oct 12 '22 19:10

Dave Alperovich