Asp.net (c #) Several misunderstandings about Session operations

Source: Internet
Author: User

1. this. Session ["username"] = null
HttpSessionState uses a collection object of the NameObjectCollection type to store user data. Therefore, using this. Session ["username"] = null only sets the value of this element to null and does not actually remove it from the Session. (Why? Dizzy ~~~ We recommend that you read C # Basic Books .)

The correct method is: this. Session. Remove ("username ");
Delete all data: this. Session. RemoveAll (); or this. Session. Clear ();

2. this. Session. Abandon ()
This method will cause the current Session to be canceled, and the system will trigger the Session_End event in Global. asax (only when Mode = InProc ).
Although the SessionID (possibly) does not change when the request is sent again, you will find that the Global. asax Session_Start event is triggered. You can also use this. Session. IsNewSession attribute to determine whether the current Session is re-created.

Because some components and controls may need to use Session information (such as using this. Session. SyncRoot for synchronization), do not use this method to clear the Session easily.

3. User Authentication
Do not use this. Session ["username"] = "ZhangSan", if (this. Session ["username"]! = Null) This method is neither secure nor unreasonable for user authentication. For more information about identity authentication, see the MSDN document or the article on rain marks.

Related Article

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.