ASP. NET session details 3

Source: Internet
Author: User

Storage of client session Status in ASP. NET

In our previous session model introduction, we can find that the session status should be stored in two places: client and server. The client is only responsible for saving the sessionid of the corresponding website, while other session information is stored on the server. In ASP, the sessionid of the client is actually stored as a cookie. If the user chooses to disable cookies in the browser settings, then he will not be able to enjoy the convenience of the session, or even access some websites. To solve the above problems, the session information storage methods of the client in ASP. NET are divided into cookie and cookieless.



In ASP. NET, by default, session information is stored on the client using cookies. If you want to use cookieless on the client to store session information, the method is as follows:



Find the current Web Application Program Open the Web. config file and find the following section:



<Sessionstate

Mode = "inproc"

Stateconnectionstring = "TCPIP = 127.0.0.1: 42424"

Sqlconnectionstring = "Data Source = 127.0.0.1; trusted_connection = yes"

Cookieless = "false"

Timeout = "20"

/>



In this section, cookieless = "false" is changed to cookieless = "true". In this way, the session information of the client is no longer stored using cookies, but stored through URLs. Close the current IE, open a new IE, and re-access the Web application, you will see something similar to the following:





Http: // localhost/mytestapplication/(ulqsek45heu3ic2a5zgdl245)/default. aspx indicates the session ID of the client. Note that this information is automatically added by IIS and does not affect the normal connection.



Storage of server session Status in ASP. NET

Preparations



To better experience the experiment, you can create a page called sessionstate. aspx and Code Add to <body> </body>.





<Scriptrunat = "server">

Sub session_add (sender as object, e as eventargs)

Session ("mysession") = text1.value

Span1.innerhtml = "session data updated! <P> your session contains: <font color = Red> "&\

Session ("mysession"). tostring () & "</font>"

End sub



Sub checksession (sender as object, EAS eventargs)

If (Session ("mysession") is nothing) then

Span1.innerhtml = "nothing, session data lost! "

Else

Span1.innerhtml = "your session contains: <font color = Red> "&\

Session ("mysession"). tostring () & "</font>"

End if

End sub

</SCRIPT>

<Formrunat = "server" id = "form2">

<Inputid = "text1" type = "text" runat = "server" name = "text1">

<Inputtype = "Submit" runat = "server" onserverclick = "session_add"

Value = "add to session state" id = "submit1" name = "submit1">

<Inputtype = "Submit" runat = "server" onserverclick = "checksession"

Value = "View session state" id = "submit2" name = "submit2">

</Form>

<Hrsize = "1">

<Fontsize = "6"> <spanid = "span1" runat = "server"/> </font>



This sessionstate. ASPX page can be used to test whether session information is lost on the current server.



Store Server session information in the process

Let's go back to the section in the web. config file:



<Sessionstate

Mode = "inproc"

Stateconnectionstring = "TCPIP = 127.0.0.1: 42424"

Sqlconnectionstring = "Data Source = 127.0.0.1; trusted_connection = yes"

Cookieless = "false"

Timeout = "20"

/>

When the mode value is inproc, it indicates that the server is using this mode.

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.