HttpHandler and ashx to implement IRequiresSessionState interface to access session information (reprint)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.