Several methods for maintaining user status in ASP. NET Programming

Source: Internet
Author: User
The following is a simple description of maintaining the user status. If you want to describe it in depth, you can write a professional book. The usage skills must be obtained in practice.

ASP. NET programming depends on the HTTP protocol, while the HTTP protocol itself is stateless. if you want to maintain the user's status, you need to adopt some technical methods, depending on the situation, you can use different methods.

To maintain the user status, follow these steps:
Query string: Adding query characters to users' web addresses can mark the user's status. Because the query characters are visible, do not store sensitive information such as passwords. As long as you provide the correct query characters, valid at any time.
Cookies: A small amount of information can be stored on the user end, but this function may not be supported by the browser or disabled. Therefore, you must check whether the client browser supports this function before use; the cookie validity period can be set to several minutes, days, or months. You can access the web site before it expires as needed.
Viewstate: The viewstate attribute is used to store variables on the page. Because viewstate is transmitted in client-server requests, too many variables should be stored; otherwise, performance may be affected. In addition, the stored variables are page-level and only valid on this page.
Session: The Current User-Defined variables are stored on the server. To distinguish users, use cookies or query characters to mark users. In addition, the Stored User variables occupy system resources. You can configure them as needed, select the location where session variables are stored, such as the current level or other machines. The function scope is the entire session process.
Application: Global variables used to store the entire web site, which are valid for all users who access the site. The function scope is the entire web.

Query characters:
Format: webform1.aspx? Name = user, which is implemented using response. Redirect: Private   Void Button#click ( Object Sender, system. eventargs E)
{
//Redisplay this page with a querystring
Response. Redirect ("Webform1.aspx? Name = user");
}

The client can use the querystring method to parse query characters: Private   Void Page_load ( Object Sender, system. eventargs E)
{
//Display the query string.
Response. Write (request. querystring ["Name"]);
}

Use COOKIE:
Cookies are suitable for storing a small amount of data on the client to indicate the user's specific information, such as name and interests. Because Browsers Do not support cookies, check whether the customer supports cookies: Private   Void Page_load ( Object Sender, system. eventargs E)
{
// Run the first time this page is displayed.
If ( ! Ispostback)
// If the browser supports cookies.
If (Request. browser. Cookies)
{
// Create a cookie.
Httpcookie cookuprefs =   New Httpcookie ( " Uprefs " );
Cookuprefs. Value =   " English " ;
// Add the cookie.
Response. Cookies. Add (cookuprefs );
}
}

Below Code You can check whether the request contains a cookie. If so, obtain the cookie value: Private   Void Page_load ( Object Sender, system. eventargs E)
{
// Run the first time this page is displayed.
If ( ! Ispostback)
// If the browser supports cookies.
If (Request. browser. Cookies)
// Check if the uprefs cookie exists
If (Request. Cookies [ " Uprefs " ] ! =   Null )
// Save the value of the cookie.
Session [ " Lang " ] = Request. Cookies [ " Uprefs " ]. Value;
}

Use viewstate:
Using viewstate to store variables is to place the variables in the form of hidden fields on the page. At the same time, you must not serialize the variables. To store complex variables, you must first convert them into strings.
The following code adds the text in textbox to the cell in the table on the page. Private   Void Button#click ( Object Sender, system. eventargs E)
{
//Add text to the view State.
Viewstate. Add (viewstate. Count. tostring (), textbox1.text );
}
Private   Void Page_load ( Object Sender, system. eventargs E)
{
If (Ispostback)
// For each item in the viewstate
Foreach (Stateitem staitem In Viewstate. values)
{
Tablerow rownew =   New Tablerow ();
Tablecell celnew =   New Tablecell ();
// Set cell text.
Celnew. Text = Staitem. value. tostring ();
// Add cell to row.
Rownew. cells. Add (celnew );
// Add row to table
Table1.rows. Add (rownew );
}
}

Use application and Session:
Application and session can be used to store any type of objects. scope can be stored in the whole webapplication or the whole session.
Note the following:
The name and type check is not performed when the application and session are used to create objects. This part of work needs to be done by developers;
To maintain session performance, you can disable this function at application or page level;
Objects Created by the application are valid only for the current process. For multi-process or multi-web application, each application maintains its own objects without affecting each other;
The Session object depends on the cookie or querystring mentioned above to indicate the user. You need to know which user the stored information belongs to. Generally, the sessionid is generated and sent to the user through the cookie, provide the sessionid in the cookie to indicate the identity.

Easy to use, but error-prone, such:

Application [ " Uname " ] =   " Wombat " ;
Response. Write (application [ " Unamme " ]);

To avoid this situation, the simplest way is to create a page variable, get the application variable in the page_load event, perform other processing, and finally update the value of the application variable in page_unload. String Mstruname =   "" ;
Private   Void Page_load ( Object Sender, system. eventargs E)
{
// Check if state variable exists.
If (Application [ " Uname " ] ! =   Null )
// Get state variable.
Mstruname = Application [ " Uname " ]. Tostring ();
// Set variable
Mstruname =   " Wombat " ;
// Use variable.
Response. Write (mstruname );
}
Private   Void Page_unload ( Object Sender, system. eventargs E)
{
//Save the state variables back.
Application ["Uname"]=Mstruname;
}

I don't know whether there are other ways to maintain the status besides these methods.

Refer:
Develop Web applications with visual basic.net and Visual C #. net
Copyright 2002

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.