Asp.Net編程中,使用者狀態維護的幾種方法

來源:互聯網
上載者:User
       下面對維持使用者狀態作簡單的描述,如果要深入描述,寫一本專業書籍也不過分,其中的提示,需要在實踐中擷取。

Asp.Net 編程依賴Http協議,而Http協議本身是無狀態的,如果要維護使用者的狀態,那麼要採用一些技術方法,根據不同的狀況,可以使用不同的方法。

維持使用者狀態的方法,如下:
查詢字串:在使用者訪問Web的地址中加入查詢字元,可以標記使用者狀態,因為查詢字元是可見的,所以不要用來儲存密碼等敏感資訊;只要提供正確的查詢字元,隨時有效。
Cookies:可以在使用者端儲存少量的資訊,但是瀏覽器可能不支援,或者關閉了此功能,所以使用前,要檢測客戶瀏覽器是否支援,才可以使用;Cookie的有效期間可以設定,幾分鐘、幾天、幾月不等,可以根據需要,在未到期前,訪問Web網站都是可用的。
ViewState:在Page頁面使用ViewState屬性儲存區變數,因為ViewState在Client-Server請求中傳輸,所以儲存的變數不宜過多,否則會影響效能;另外,儲存的變數是頁面級的,只在本頁面有效。
Session:在Server端儲存目前使用者定義的變數,要分辨使用者,要使用Cookie或者查詢字元來標記使用者,另外,儲存的使用者變數要佔用系統資源,可以根據配置,選擇Session變數儲存的位置,如本級,或者其他機器。作用範圍是整個會話過程。
Application:用來儲存整個Web網站的全域變數,對所有訪問使用者有效。作用範圍是整個Web。

使用查詢字元:
格式如webform1.aspx?name=user,使用方法Response.Redirect來實現: private void Button1_Click(object sender, System.EventArgs e)
{
// Redisplay this page with a QueryString
Response.Redirect("Webform1.aspx?name=user");
}

用戶端可以使用QueryString方法來解析查詢字元: private void Page_Load(object sender, System.EventArgs e)
{
// Display the query string.
Response.Write(Request.QueryString["name"]);
}

使用Cookie:
Cookie適合在用戶端儲存少量的資料,用來標示使用者的特定資訊,如姓名,愛好等等;因為瀏覽器可以不支援Cookie,所以使用時,應該先檢測客戶是否支援cookie: 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);
}
}

下面代碼可以檢查請求是否包含cookie,如果有,那麼擷取cookie的值: 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;
}

使用ViewState:
使用ViewState儲存變數,是把變數以隱藏欄位的形式放在頁面中,同時要求變數可以別序列化,如果要儲存複雜的變數,那麼需要先把變數轉換為字串。
下面的代碼是把TextBox中的文本加入到頁面的Table中的Cell。 private void Button1_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);
}
}

使用Application和Session:
使用Application和Session可以儲存任何類型的對象,Scope在整個的WebApplication或者整個Session。
注意以下幾點:
Application和Session建立對象時不做名稱和類型檢查,這部分工作需要開發人員來做;
維持Session影響效能,可以在Application或者Page Level來關閉此功能;
Application建立的對象,只在當前進程有效,對於多進程或者多Web Application,每個Application維持自己的對象,不相互影響;
Session對象,依賴前面講的Cookie或者QueryString來標示使用者,需要知道儲存的資訊是哪個使用者的,一般是採用產生SessionID,通過Cookie發給使用者,使用者訪問時,在Cookie中提供這個SessionID,說明身份。

使用簡單,但是容易出錯,如:

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

為了避免這種狀況,最簡單的辦法是建立一個頁面的變數,在Page_Load事件中擷取Application變數,然後作其他處理,最後在Page_UnLoad中更新Application變數的值。 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;
}

不知道除了這些方法外,還有沒有其他維持狀態的辦法。

參考:
Develop Web Applications with visual Basic.net and visual C#.net
Copyright 2002

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.