Often, we often use the session to determine if a user is logged in. There are also some temporary, important data to try to store in the session.
In the page we can easily get the value of the session, but in the class you will encounter some problems. Also know by the following method to get.
system.web.httpcontext.current.session["userinfo"];
But today the method also failed. To do a small application, you need to implement IHttpHandler, but also need to use the user's identity. But there is no way to find the value of the session in this class, exposing
System.Web.HttpContext.Current.Session is null
Why is the session going to be empty? Thought for a long time also did not think. Looking for a long time, only to find the expert's guidance, the problem has been solved.
Workaround:
In the implementation of IHttpHandler, but also to implement the IRequiresSessionState interface, its namespace is: System.Web.SessionState.
Public class Watermarkhandler:ihttphandler, system.web.sessionstate.irequiressessionstate{ // Code in Watermarkhandler }
Continue tracking:
Why do you want to implement this interface? What is this interface for? Continue tracking, MSDN gives the final explanation.
IRequiresSessionState
Specifies that the destination HTTP handler requires read and write access to the session-state value. This is a markup interface and there is no method.
Role:
Implement the IRequiresSessionState interface in a custom HTTP handler to determine whether the handler needs to have read and write access to the session-state value
So remember oh, if you want to access the session in a custom HTTP handler, be sure to implement this interface.
HttpHandler and ashx to implement IRequiresSessionState interface to access session information (reprint)