1
Greeting to all.
I am developing an application in Asp.NET MVC5 and found a following problem:
The application needs me to log in from any screen it accesses via browser. To decrease the amount of code lines in my views I created a Actionfilter which will serve both to validate the login and to popular the Sessions that I need to keep on my site for proper functioning of the requester.
Following this logic, when making the first access to the site the sessions are filled in the correct way, until then no problem, however when we updated for example the user profile to ensure more access to it we realized that the sessions were not being updated automatically between the navigation of the screens (Actionfilter is configured to run before the execution of the actions). Realizing this, I thought of a workaround, creating a button that would call an action to update these sessions and everything ok, right? Wrong!
When I use for example:
public ActionResult AtualizaPerfil()
{
...
HttpContext.Session["PerfilUsuario"] = idPerfil;
...
}
within my action I assign a value to the session and at the end of the action I redirect to a view return RedirectToAction("Index", "Home");
that has Actionfilter bound [ControleAcessoActionFilter]
.
Within Actionfilter, I have the Method to follow:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
...
if (HttpContext.Current.Session["PerfilUsuario"] == null)
{
filterContext.HttpContext.Session.Add("PerfilUsuario", idPerfil);
}
...
}
Lo that we arrived at the problem, the Session["Profile"] of my view did not assign the value to my Httpcontext.Current.Session["Profile"], I found it very strange since both have equal origin following the logic of this post here from Stackoverflow International.
Would anyone have any other explanation for this case?
Aren’t you working in different contexts?
if (HttpContext.Current.Session["PerfilUsuario"] == null)
andfilterContext.HttpContext.Session
?– Leandro Angelo
I don’t think @Leandroangelo, since httpContext is theoretically inherited by filterContext since it receives all the parameterization of the action before it is executed.
– Angelo Simonato