Use the session solution in the ashx file

  • 2020-05-19 04:40:11
  • OfStack

If you want to secure your data, you can use session validation in ashx.
For example, if you use jquery in index.aspx to call back ashx data, then session["checked"]="true" in index.aspx page_load, then verify the existence of session in ashx.

However, the following steps are required to obtain the content of session in ashx:

(1) import the namespace

using System; 
using System.Web; 
using System.Web.SessionState;// The first 1 Step: import this namespace 

Implementation interface

publicclassMyWeb:IHttpHandler change this to read:

publicclassMyWeb:IHttpHandler,IRequiresSessionState// step 2: implement the interface and you can use Session as usual

Call method

HttpContext.Current.Session ["Session variable name "]

Related articles: