Session variables in action filter
32,615
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);
}
}
Author by
Jeff
Updated on July 09, 2022Comments
-
Jeff almost 2 years
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? -
Jeff over 11 yearsDoh! It's the smallest things that get you sometimes... Also, I edited your answer to change these lines:
public override void OnActionExecuting(ActionExecutedContext context)
base.OnActionExecuted(context);
To sayExecuting
also -
Dave Alperovich over 11 yearsalways the small things that kill! big ones are easy to find.
-
Jim Moriarty over 10 yearsThis code doesn't even compile! The method signature is wrong.
-
Dave Alperovich almost 8 years@dvallejo, I guess you missed the point of my answer. I didn't really scan OP's code. I simply pointed out the part that I was clearly wrong. I assumed the rest worked. The signature and compilation doesn't have anything to do with OP using the wrong override.
-
Mwas about 5 yearsI am unable to use this approach in .netcore, I am getting
Cannot apply indexing with [] to an expression of type iSession
. What can I do? -
Dave Alperovich about 5 years@Mwas, this should technically be posted as a new question pertaining to .NET Core, but to answer your question, in Core, the new filter should inherit from the IActionFilter interface