1

Just a quick question:

How come i can't access Session[key] on my ActionFilterAttribute class?

(VS always change it to sessionChecker if i press [ )

How am i supposed to get and check the session value? I need to retrieve the ID that is stored in the session and use it to compare for something.

Here is my code:

Login(post):

    [HttpPost]
    [ActionName("login")]
    public ActionResult login_post(string uname, string pword)
    {

        using (EmployeeContext emp = new EmployeeContext())
        {
            //h student log = new student();
            int success = emp.login.Where(x => x.username == uname && x.password == pword).Count();
            if (success == 1)
            {
                int id = (from logs in emp.login
                          join rol in emp.roles on logs.role equals rol.id
                          where logs.username == uname
                          select logs.id).First();

                FormsAuthentication.SetAuthCookie(uname, false);

               Session["Login"] = id;

                return RedirectToAction("Details", "Enrollment", new { id = id });
            }
            return View();
        }
    }

ActionFilterAttribute class

    public class CustomChecker: ActionFilterAttribute
    {
    public string test { get; set; }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        int t_id;

        t_id = //sessionChecker keeps on appearing whenever i try typing "Session". I get red line from it. How do i access the session?
Carlos Miguel Colanta
  • 2,685
  • 3
  • 31
  • 49

1 Answers1

7

You need to access it from the filterContext (Session is not a property of ActionFilterAttribute)

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
  int id = (int)filterContext.HttpContext.Session["Login"];
}
  • my i know why? isn't `Session` supposed to be at the top most and should be accessible anywhere from the application? – Carlos Miguel Colanta Sep 10 '15 at 02:05
  • Because you attribute inherits from class `ActionFilterAttribute` which does not have a property named `Session`. Note also you will want to check for `null` before you assign the value (because it may be `null` the first time you access this method) –  Sep 10 '15 at 02:07
  • Alright, one last question. If a session timed out, what's its value going to be? back to null? – Carlos Miguel Colanta Sep 10 '15 at 02:09
  • It will be `null` so you will need to check for that and call the database again to get the value, But you would be far better of storing the value in the cookie (e.g. if your using `Identity`, then use `Claims`) –  Sep 10 '15 at 02:11
  • thank you so much for the answers. can you link me a good material wherein i can read something regarding `Claims` ? – Carlos Miguel Colanta Sep 10 '15 at 02:12
  • Lots of articles on the web and question/answers on SO, but you could look at [this](http://stackoverflow.com/questions/28335353/how-to-extend-available-properties-of-user-identity) and [this](http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-2) to get you started. And if you want your own `MembershipProvider`, then [this article](http://www.primaryobjects.com/CMS/Article147) –  Sep 10 '15 at 02:26