브라우저는 사용자 시스템상에 쿠키들을 관리할 책임이 있습니다. 쿠키들은 HttpResponse 객체를
통해서 브라우저에 보냅니다. Page 클래스의 Response 프로퍼티 처럼 HttpResponse 객체를 접근할 수 있습니다. 브라웢에 보내기를 원하는 어떤 쿠키들도 collection 에 추가될 겁니다. 쿠키를 생성할 때, 쿠키의 명과 값을 명시해야합니다. 브라우저가 서버에서 request 할 때 , 서버는 request 와 함께 쿠키들을 보냅니다. asp.net 어플리케이션에서 HttpRequest 객체를 이용하여 쿠키들을 읽을 수 있습니다. 마치 page 클래스의 request 프로퍼티 처럼 가능합니다. 하나의 text box 를 추가하고 2개의 버튼들과 하나의 label 을 웹페이지에 추가했습니다. textbox 은 쿠키정보를 inputting 하기 위한 용도입니다. 추가 버튼을 누름으로 인해 샘플 어플리케이션은 새로운 쿠키를 생성할 겁니다. View 버튼을 클릭하면 우리는 생성된 쿠키를 확인 하실 수 있습니다. 쿠키 만기일은날짜는 2006-10-1로 셋팅 했습니다.
protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Response.Cookies["MyCookie"]["Data"] = TextBox1.Text; Response.Cookies["MyCookie"]["Time"] = DateTime.Now.ToString("G"); Response.Cookies["MyCookie"].Expires=DateTime.Now.AddMonths(1); Label1.Text = "Cookie created!<p>" + "Your cookie contains:<font color=red>" + Request.Cookies["MyCookie"]["Data"] + "<br>" + Request.Cookies["MyCookie"]["Time"] + "</font>"; Response.Cookie("MyCookie").Expires=DateTime.FromString("2006-10-1"); } protected void Button2_Click(object sender, EventArgs e) { if (Request.Cookies["MyCookie"] == null) Label1.Text = "There is no cookie:"; else Label1.Text = "Your cookie contains:" + "<font color=red>" + Request.Cookies["MyCookie"]["Data"] + "<br>" + Request.Cookies["MyCookie"]["Time"] + "</font>"; } |
aspx 페이지 입니다.
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body>
|
전체적인 code behind 는 아래와 같습니다.
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) } protected void Button1_Click(object sender, EventArgs e) } |
감사합니다.
==================================
카페 : http://cafe.daum.net/aspdotnet
작성자 : 심재운
메일 : shimpark@gmail.com
==================================