自定义授权过滤器中的访问会话

时间:2014-03-26 07:06:02

标签: c# asp.net-mvc-4

如何在自定义授权过滤器中使用Session["userName"]

void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
{            
    string userName = Session["userName"]; // Shows error "The name 'Session' does not exist in the current context              

    base.OnAuthorization(filterContext);
}

1 个答案:

答案 0 :(得分:7)

Session字典可以通过以下方式获得:

void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
{            
    string userName = filterContext.HttpContext.Session["userName"];               

    base.OnAuthorization(filterContext);
}