Generally, session is used to determine whether a user is logged on. Some temporary and important data is also stored in the session.
On the page, we can easily get the session value, but some problems will occur in the class. You can also obtain it through the following method.
System. Web. httpcontext. Current. session ["userinfo"];
But today, this method also fails. To create a small application, you must implement ihttphandler and use the user ID. However, the session value cannot be found in this class.
System. Web. httpcontext. Current. Session is null
Why is the obtained session empty? I haven't figured it out for a long time. After looking for a long time, I found the expert's guidance and solved the problem.
Solution:
While implementing ihttphandler, you must also implement the irequiressessionstate interface. Its namespace is system. Web. sessionstate.
Public class watermarkhandler: ihttphandler, system. Web. sessionstate. irequiressessionstate {}
Continue tracing:
Why is this interface implemented? What is the purpose of this interface? Continue tracing. msdn gave the final explanation.
Irequiressessionstate
Specify the target HTTP handler to have read and write access to the session status value. This is a tag interface and there is no way.
Purpose:
Implement the irequiressessionstate interface in the Custom HTTP handler to determine whether the handler needs to have read and write access to the session status value.
So remember, if you want to access the session in a custom HTTP handler, you must implement this interface.