Learning from ASP. NET session

Source: Internet
Author: User

Objective

Recently wrote a similar mall site, using the session to maintain the user's login status, before the full understanding of the session, take this opportunity to thoroughly understand the next session, the content is based on their own network of some information and Daniel's blog finishing, there are some of their own understanding.

1. What is a session

Sessions, called Session state, are the most common state in a web system and are used to maintain some information related to the current browser instance.

2.SessionID

When the user first establishes a connection to the Web server, the server distributes a sessionid as an identity to the current access user. SessionID is a random string of 24 characters. Each time the user submits the page, the browser will include the SessionID in the HTTP header to the Web server, so that the Web server can distinguish between the client on the current request page. This sessionid is stored on the client side and belongs to the client session. (ASP. NET if you access a background processing page, you can use Session.SessionID to take a value. The client session is stored by default in the form of a cookie, so when the user disables the cookie, the server is not SessionID.

3.Session Mode and Session Provider

InProc: session state is disabled.

StateServer: session state is processing the ASP. NET worker process.

SQL Server: session state is using the out-of-process ASP. NET State Service to store state information.

Custom: session state is using an out-of-process SQL Server database to store state information.

OFF: session state is using a custom data store to store session state information.

Summarize:

    • 1, InProc Session provider is the fastest, because all the data exist in the application's memory, the Session data in the IIS restart, or the site is recycled, you can use this mode on the less user-intensive site, But don't use it under the Web farm.
    • 2, State Server mode: Session data is stored in the aspnet_state.exe application, he saves the session data outside the Web service, so the Web service problems will not affect his session data, It is necessary to serialize the object before storing the session data to StateServer, which we can safely use in the Web farm.
    • 3. SQL Server Mode: He saves the session data to SQL Server, we need to provide a connection string, we also need to serialize the object when stored, this mode is very useful in the production environment of the actual Web farm.
    • 4, we can also use the custom mode, when we need to use an existing table to store session data, in the custom mode, we can also create a custom session ID, but do not recommend yourself to implement provider, we recommend the use of third-party provider.

4. Introduction to use

Configuration mode in Web. config mode: mode, timeout expiration (minutes)

<system.web>    <!--InProc is the default session mode---    <sessionstate mode="InProc  "timeout=" ></sessionState></system.web>

Sesion Reading and writing

   var  ss=New  User ();   session["user"]=ss; // Write   var  bb= session["user" as user;   Read

Reference Address:

The source of the article: "Translation" on the session of ASP. Uliian Bamboo

The ASP. NET session recognizes how the enhanced-session is actually stored, you know? dz45693

Asp. NET session of the individual talking about Flyelephant

Original link

Learning from ASP. NET session

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.