大型Web 網站 Asp.net Session到期你怎麼辦

來源:互聯網
上載者:User

標籤:blog   使用者   覆蓋   fonts   article   跳轉   tar   sdn   art   

在 WEB 系統中。 我們通常會用session來儲存一些簡單可是卻非常重要的資訊。比方Asp.net中常常會用Session來儲存使用者登入資訊,比方UserID。為瞭解決 WEB場大家採用了把session存在DB中,session到期大家一般都採用頁面跳轉,即再次登入,login後又返回頁面。

個人認為以上設計不是非常好, 對於web場,假設我們把session存在DB那麼新能應該比存記憶體要慢。所以推薦用分布式緩衝的方式來存取Session。 對於Session到期我建議採用cookie來做。在大型網站中Session應該慎用,畢竟它佔用server的內容。一個人使用者session假設佔用1k的空間,那麼100W使用者同一時候線上 Session要佔用多大空間. 曾經我把userID 直接存cookie會有瀏覽器串cookie的問題,比方我用IE login use1,用FF login user2,發現後面login的user資訊會覆蓋前面login user的值。

回來發現session到期了,可是sessionID還在,而且該值在cookie裡面。

實現code 例如以下:

核心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 class WebForm1 : System.Web.UI.Page    {        protected void btnSet_Click(object sender, EventArgs e)        {            Session["name"] = "majiang";            this.lblSet.Text = "Session ID:" + Session.SessionID.ToString();        }        protected void btnGet_Click(object sender, EventArgs e)        {            labGet.Text = "Session ID:" + Session.SessionID.ToString();            if (Session["name"] != null)            {                labGet.Text += "<br/>" + Session["name"].ToString();            }        }        Dictionary<string, string> dict = new Dictionary<string, string>();        protected void Page_Load(object sender, EventArgs e)        {            dict.Add("1", "majiang");            dict.Add("2", "Gavin");        }        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);            }        }        protected void btnSetwithCookie_Click(object sender, EventArgs e)        {            UserID = this.txtuserID.Text.Trim();            this.labsetCookie.Text = Session.SessionID.ToString();        }        protected void btnGetWithCookie_Click(object sender, EventArgs e)        {            this.labGetCookie.Text = "Session ID:" + Session.SessionID.ToString();            labGetCookie.Text += "<br/>" + dict[UserID].ToString();        }    }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SessionTest.WebForm1" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">        <div>            <asp:Button ID="btnSet" runat="server" Text="Set Session" OnClick="btnSet_Click" />            <asp:Button ID="btnGet" runat="server" Text="Get Session" OnClick="btnGet_Click" />            <br />           SET: <asp:Label ID="lblSet" runat="server" Text=""></asp:Label>            <br />           Get: <asp:Label ID="labGet" runat="server" Text=""></asp:Label>        </div>        userID:<asp:TextBox ID="txtuserID" runat="server"></asp:TextBox>        <div>            <table>                <tr><td><asp:Button ID="btnSetwithCookie" runat="server" Text="Set With Cookie" OnClick="btnSetwithCookie_Click" /></td><td><asp:Button ID="btnGetWithCookie" runat="server" Text="Get With Cookie" OnClick="btnGetWithCookie_Click" /></td></tr>            <tr><td><asp:Label ID="labsetCookie" runat="server"></asp:Label> </td><td><asp:Label ID="labGetCookie" runat="server"></asp:Label></td></tr>            </table>        </div>    </form></body></html>

實現的效果

看看HTTP的請求:



大型Web 網站 Asp.net Session到期你怎麼辦

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.