ASP.NET頁面之間傳值的方式之Cookie(個人整理)

  Cookie

Cookie 提供了一種在 Web 應用程序中存儲用戶特定信息的方法。例如,當用戶訪問您的站點時,您可以使用 Cookie 存儲用戶首選項或其他信息。當該用戶再次訪問您的網站時,應用程序便可以檢索以前存儲的信息。所以Cookie也可以在頁面間傳遞值。Cookie通過HTTP頭在瀏覽器和服務器之間來回傳遞的。Cookie只能包含字符串的值,如果想在Cookie存儲整數值,那麼需要先轉換爲字符串的形式。

  與Session一樣,其是什對每一個用戶而言的,但是有個本質的區別,即Cookie是存放在客戶端的,而session是存放在服務器端的。而且Cookie的使用要配合ASP.NET內置對象Request來使用。

  優點:1.使用簡單,是保持用戶狀態的一種非常常用的方法。比如在購物網站中用戶跨多個頁面表單時可以用它來保持用戶狀態。

  缺點:1.常常被人認爲用來收集用戶隱私而遭到批評。

     2.安全性不高,容易僞造。

  例子:(1)a.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="a.aspx.cs" Inherits="WebApplication.a" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>

      (2)a.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication
{
    public partial class a : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            var aa = TextBox1.Text.Trim();
            var cookie = new HttpCookie("UserName", aa);
            cookie.Expires = DateTime.Now.AddMinutes(1);//設置cookies的作用時間是一分鐘
            Response.Cookies.Add(cookie);
            Response.Redirect("b.aspx");//跳轉到b頁面

        }
    }
}

     (3)b.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="b.aspx.cs" Inherits="WebApplication.b" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
    </form>
</body>
</html>

    (4)b.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication
{
    public partial class b : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = Request.Cookies["UserName"] != null ? Request.Cookies["UserName"].Value : "Cookies值已經失效";
            //三元表達式,等同於下面註釋的代碼

            //if (Request.Cookies["UserName"] != null)
            //{
            //    Label1.Text = Request.Cookies["UserName"].Value;
            //}
            //else
            //{
            //    Label1.Text = "Cookies值已經失效";
            //}
        }
    }
}

    

 

ps:此文章是本人蔘考網上內容加上自己的理解整合而成,如無意中侵犯了您的權益,請與本人聯繫。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章