What do you do when a large web site ASP.

Source: Internet
Author: User

In the WEB system, we usually use the session to save some simple but important information. For example, ASP. NET often uses session to save user login information, such as UserID. In order to solve the Web farm everyone uses the session exists in DB, the session expires everyone generally use page jump, that is, login again, login and return to the page. Personally think that the above design is not very good, for the Web farm, if we put the session db so the new can be slower than memory, it is recommended to use a distributed cache to access the session. I recommend using cookies for session expiration. In large Web site session should be used with caution, after all, it occupies the contents of the server, a person user session if Occupy 1k of space, then 100W users at the same time online session to occupy how much space. Before I put the userid directly into the cookie there is a browser string cookie problem, such as I use IE login use1, with FF login user2, found that the user information behind login will overwrite the value of the previous login user. Came back to find the session expired, but SessionID is still there, and the value is in the cookie.

The implementation code is as follows:

Core code:

String UserID
{

Get
{
if (session["UserID"]! = NULL)
{
return session["UserID"]. ToString ();
}
if (request.cookies[session.sessionid.tostring ()] = null)
{

String Cv=request.cookies[session.sessionid]. Value;
session["UserID"] = CV;
return CV;
}
return string. Empty;
}
Set
{
session["UserID"] = value;
String key = Session.SessionID.ToString ();
HttpCookie KC = new HttpCookie (key, value);
Kc. HttpOnly = true;
RESPONSE.COOKIES.ADD (KC);
}
}

 Public Partial classWebForm1:System.Web.UI.Page {protected voidBtnset_click (Objectsender, EventArgs e) {session["name"] ="Majiang";  This. Lblset.text ="Session ID:"+Session.SessionID.ToString (); }        protected voidBtnget_click (Objectsender, EventArgs e) {Labget.text="Session ID:"+Session.SessionID.ToString (); if(session["name"] !=NULL) {Labget.text+="<br/>"+ session["name"].            ToString (); }} Dictionary<string,string> dict =Newdictionary<string,string>(); protected voidPage_Load (Objectsender, EventArgs e) {Dict. ADD ("1","Majiang"); Dict. ADD ("2","Gavin"); }        stringUserID {Get            {                if(session["UserID"] !=NULL)                {                    returnsession["UserID"].                ToString (); }                if(Request.cookies[session.sessionid.tostring ()]! =NULL)                {                                         stringcv=Request.cookies[session.sessionid].                    Value; session["UserID"] =CV; returnCV; }                return string.            Empty; }            Set{session["UserID"] =value; stringKey =Session.SessionID.ToString (); HttpCookie KC=NewHttpCookie (key, value); Kc. HttpOnly=true;            RESPONSE.COOKIES.ADD (KC); }        }        protected voidBtnsetwithcookie_click (Objectsender, EventArgs e) {UserID= This. TxtuserID.Text.Trim ();  This. Labsetcookie.text =Session.SessionID.ToString (); }        protected voidBtngetwithcookie_click (Objectsender, EventArgs e) {             This. Labgetcookie.text ="Session ID:"+Session.SessionID.ToString (); Labgetcookie.text+="<br/>"+Dict[userid].        ToString (); }    }
View Code
<%@ Page Language="C #"AutoEventWireup="true"codebehind="WebForm1.aspx.cs"Inherits="Sessiontest.webform1" %><!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Headrunat= "Server">    <title></title></Head><Body>    <formID= "Form1"runat= "Server">        <Div>            <Asp:buttonID= "Btnset"runat= "Server"Text= "Set Session"OnClick= "Btnset_click" />            <Asp:buttonID= "Btnget"runat= "Server"Text= "Get Session"OnClick= "Btnget_click" />            <BR/>SET:<Asp:labelID= "Lblset"runat= "Server"Text=""></Asp:label>            <BR/>Get:<Asp:labelID= "Labget"runat= "Server"Text=""></Asp:label>        </Div>UserID:<Asp:textboxID= "Txtuserid"runat= "Server"></Asp:textbox>        <Div>            <Table>                <TR><TD><Asp:buttonID= "Btnsetwithcookie"runat= "Server"Text= "Set with Cookie"OnClick= "Btnsetwithcookie_click" /></TD><TD><Asp:buttonID= "Btngetwithcookie"runat= "Server"Text= "Get with Cookie"OnClick= "Btngetwithcookie_click" /></TD></TR>            <TR><TD><Asp:labelID= "Labsetcookie"runat= "Server"></Asp:label> </TD><TD><Asp:labelID= "Labgetcookie"runat= "Server"></Asp:label></TD></TR>            </Table>        </Div>    </form></Body></HTML>
View Code

The effect of the implementation

Look at the HTTP request:

What do you do when a large web site ASP.

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.