在ashx文件中使用session的解决思路

时间:2022-09-25 20:24:43

如果你要保证数据的安全性,你可以在ashx中使用session验证。
如:你的index.aspx中使用jquery回调ashx数据,那么在index.aspx page_loadsession["checked"]="true",在ashx中验证session是否存在。

ashx中要获取session内容还需如下几步

①导入命名空间

复制代码代码如下:


using System; 
using System.Web; 
using System.Web.SessionState;//第一步:导入此命名空间


②实现接口

publicclassMyWeb:IHttpHandler将此改为如下:

publicclassMyWeb:IHttpHandler,IRequiresSessionState//第二步:实现接口 到此就可以像平时一样用Session了

③调用方法

HttpContext.Current.Session["Session变量名称"]