我无法访问 ActionFilterAttribute 内的会话

Car*_*nta 1 asp.net-mvc session

只是一个简单的问题:

为什么我无法访问Session[key]我的 ActionFilterAttribute 类?

(如果我按 [ ,VS 总是将其更改为 sessionChecker )

我应该如何获取和检查会话值?我需要检索ID存储在会话中的 ,并使用它来比较某些内容。

这是我的代码:

登录(发布):

    [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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

ActionFilterAttribute 类

    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?
Run Code Online (Sandbox Code Playgroud)

小智 7

您需要从filterContext( Sessionis not a property of ActionFilterAttribute)访问它

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
  int id = (int)filterContext.HttpContext.Session["Login"];
}
Run Code Online (Sandbox Code Playgroud)