.net中如何使用cookie

來源:互聯網
上載者:User

標籤:

比如建立一個名為aspcn,值為灌水小魚的cookie

HttpCookie cookie = new HttpCookie["aspcn"];
cookie.Value = "灌水小魚";
Response.AppendCookie(cookie);

取出Cookie值也很簡單

HttpCookie cookie = Request.Cookies["aspcn"];
cookieValue = cookie.Value;

在一個Cookie中儲存多個資訊,那也沒有問題。比如在名為aspcn的cookie下加多個資訊

HttpCookie cookie = new HttpCookie("aspcn");
cookie.Values.Add("webmaster","灌水小魚");
cookie.Values.Add("writer","beige");
cookie.Values.Add("LinkColor","blue");
Response.AppendCookie(cookie);

取出資訊也一樣簡單

HttpCookie cookie = Request.Cookies["aspcn"];
value1 = cookies.Values["webmaster"];
value2 = cookies.Values["writer"];

 

HttpCookie cookie = Request.Cookies[strKey]; 
if(null == cookie) 
{ 
//cookie不存在 
} 
先看看有沒有寫入用戶端 



cookie=new HttpCookie("www.xx.org"); 
cookie.Values.Add("UserType", ddlLoginType.SelectedItem.Value); 
cookie.Values.Add("UserName", tbUserName.Text.Trim()); 
cookie.Values.Add("Password", tbPassword.Text.Trim()); 
cookie.Values.Add("Access",i.ToString()); 
Response.AppendCookie(cookie); 
//檢查COOKIE是否已經寫入瀏覽器 
cookie = Request.Cookies["www.xx.org"]; 
if(cookie==null||cookie.ToString()=="") 
{ 
//用session儲存 
LoginUser loginUser = new LoginUser(); 
loginUser.UserName = tbUserName.Text.Trim(); 
loginUser.Password = tbPassword.Text.Trim(); 
loginUser.UserType = ddlLoginType.SelectedItem.Value; 
loginUser.Access = i; 
Session["www.xx.org"] = loginUser; 
} 

我以前的代碼,沒有問題的 




>>>頁面僅僅是重新整理了一次 

if you debug, does the code get executed at all? by the way, are you doing any validation? 

>>>然後是刪除 

>>>Response.Cookies.Remove("MyCookie"); 
>>>Response.Redirect("....") // 轉向  

>>>然後是刪除 

HttpCookie cookie = Request.Cookies["myinfo"];
        if (cookie != null)
        {
            cookie.Expires = System.DateTime.Now.AddDays(-1);
        }
        Response.Cookies.Add(cookie);

 

Response.Cookies["LoginCode"].Expires =DateTime.Now.AddHours(1);
這是有效期間為1小時,同樣的方法可以類推成幾分鐘,幾秒等等。

.net中如何使用cookie

聯繫我們

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