The principle of session in ASP.net and its application in detail _ practical skills

Source: Internet
Author: User
Tags sessions server memory
Introduction to Session, features
--------------------------------------------------------------------------------
1.Session is one of the most common states in a Web session.
2.Session provides a way to keep information in server memory. He can store any data type, including custom objects.
3. The seesion of each client is stored independently.
4. The session information is saved as long as the SessionID cookie is not lost throughout the conversation.
5.Session cannot be accessed across processes and can only be accessed by users of that session. The ID of the fetch session data should be saved in a cookie to the visitor's browser's cache.
6. When the session terminates, or expires, the server clears the sessions object.
7.Session is often used to save the ID of the logged-on user.
The data saved by 8.Session is a cross page global type.
--------------------------------------------------------------------------------
use of session
--------------------------------------------------------------------------------
Here I write an example for easy elaboration below.
Copy Code code as follows:

<title></title>
<script src= "Scripts/jquery-1.4.1.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
function Getsessionclick (action) {//It is to know which submit button is clicked
$ ("#hidlgc"). Val (""); Empty hidden values
$ ("#hidlgc"). Val (action); Assigning values to a hidden control
}
</script>
<body>
<form id= "Form1" method= "Post" action= "mysession.aspx" >
<table>
<tr>
<td> account: </td><td><input type= "text" name= "Txtuid"/></td> "
</tr>
<tr>
<td> Password: </td><td><input type= "password" name= "Txtpwd"/></td>
</tr>
<tr>
&LT;TD colspan= "2" >
<input type= "hidden" value= "id=" HIDLGC "name=" Hidlgclick "/>"
<input onclick= "Getsessionclick (' Lgclick ')" type= "Submit" value= "Login"/>
<input type= "Submit" onclick= "Getsessionclick (' getsession ')" value= "Get Session"/>
<input type= "Submit" onclick= "Getsessionclick (' Backlg ')" value= "Exit Login"/>
</td>
</tr>
</table>
</form>
</body>

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
Write user ID to session
if (request.form["hidlgclick"] = = "Lgclick")
{
if (request.form["Txtuid"]. ToString () = = "Admin" &&request.form["Txtuid"]. ToString () = "admin")//Judge User Login
{
session["userName"] = request.form["Txtuid"]. ToString (); Save User ID to session
Response.Write (session["UserName"). ToString () + "---click Login"); Get session and write to page
}
}
Get session
if (request.form["hidlgclick"] = = "GetSession")
{
if (session["userName"]!= null)
{
Response.Write (session["UserName"). ToString () + "---Click to get Session"); Get session and write to page
}
}
Canceling the current session is equivalent to logging off (logging out).
if (request.form["hidlgclick"] = = "BACKLG")
{
Session.Abandon ();
}
}

Copy Code code as follows:

<system.web>
<sessionstate timeout= ></sessionState> <!---set the expiration time for the session in minutes-->


Session principle (illustrated in the example above)
--------------------------------------------------------------------------------
first, how to store the session, extracted?
1. There is a session pool on the server side that stores the data for each user submitting the session, and the session is "hand over" for each client (or browser instance), and when the user first connects to the Web server, The server distributes a sessionid as an identity to the user. SessionID is a 24-character random string. Each time the user submits the page, the browser will include the SessionID in the HTTP header and submit it to the Web server, so that the Web server can distinguish which client is on the current request page, and this sessionid is stored in the client's memory in a cookie way. If you want to get the data from the session pool, the server returns the data based on the unique SessionID identity submitted by the client.
2. Enter the correct account password, click Login, the page will output "Admin---Click Login"
Second, the session pool in each client data is how to store?
1. The data stored in the session pool is a global type of data that can be accessed across pages, with only unique data stored in each SessionID, such as: first you set this up: session["UserName"]= "admin", Then you set up: session["UserName"]= "123" when the session hasn't expired yet, so the SessionID doesn't change, but the data in the sessions pool is overwritten. At this point the value of session["UserName" is "123", not the other.
Data in a 2.Session pool cannot be accessed across processes. such as: Open login.aspx page Write session["userName"]= "admin"; then the login page does not close, that is, this session does not end, where you open a Login.aspx page in another browser session[" UserName "]=null
3. Enter account password, click the login page Output "Admin---Click to login", if the next click Get Session button, then the page output only "Admin---Click to get Session", if the page does not close, open another browser, click Get Session button , the page will not be able to.
the declaration cycle and destruction of the session
1.session Storage Data timing is a scrolling timing method. Specifically, if you open the write session, from the write start, this page if the operation has not been submitted, the default time is 20 minutes, 20 minutes after the session is automatically destroyed by the server, such as the submission of the operation, the server from the time of the submission and so on, and so on, until the set
2. You can set the time for the session to be destroyed. The code above is mentioned.
The data stored in the session is at the service end, and each user, such as the login operation, must be written to the session data, so it is recommended to use the session carefully, is less.
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.