Asp.Net te Cookie Kullanımı
Merhaba arkadaşlar bu makalemizde asp.net te cookie ile ilgili basit bir örnek yapacağız.
Kısaca cookie; client(istemci) tarafında barındırılan ve içerisinde belirli bilgileri düz metin olarak tutan küçük boyutlu dosyalardır. Sunucu tarafından gönderilen çerezler, istemci tarafına kaydedilmektedir. Çerezlerde boyut olarak 4KB’lık bir sınır vardır.
Örneğimizde personel bilgilerini çerez olarak 30 dakika süre için browser da kaydedip depoluyoruz.
Sekil 1
Sekil 2
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace aspnet_cookies
{
public partial class WebForm1 : System.Web.UI.Page
{
int userId;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindCookies();
}
}
void bindCookies()
{
if (userId == 1)
{
Response.Cookies["Person"][("Username")] = "Bahadir Sahin";
Response.Cookies["Person"]["Department"] = "IT";
Response.Cookies["Person"]["Contact"] = "+1 2452112";
Response.Cookies["Person"]["E-Mail"] = "basahin23@hotmail.com";
}
if (userId == 2)
{
Response.Cookies["Person"][("Username")] = "Melissa Parker";
Response.Cookies["Person"]["Department"] = "IT";
Response.Cookies["Person"]["Contact"] = "+1 8545190";
Response.Cookies["Person"]["E-Mail"] = "melparker@gmail.com";
}
Response.Cookies["Person"].Expires = DateTime.Now.AddMinutes(30);
}
protected void btnClick_Click(object sender, EventArgs e)
{
userId++;
bindCookies();
txtUserId.Text = userId.ToString();
txtUser.Text=Response.Cookies["Person"][("Username")];
txtDepartment.Text = Response.Cookies["Person"]["Department"];
txtContact.Text = Response.Cookies["Person"]["Contact"];
txtMail.Text = Response.Cookies["Person"]["E-Mail"];
}
protected void btnNext_Click(object sender, EventArgs e)
{
userId++;
userId++;
bindCookies();
if (userId == 2)
{
txtUserId.Text = userId.ToString();
txtUser.Text = Response.Cookies["Person"][("Username")];
txtDepartment.Text = Response.Cookies["Person"]["Department"];
txtContact.Text = Response.Cookies["Person"]["Contact"];
txtMail.Text = Response.Cookies["Person"]["E-Mail"];
}
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
userId=1;
txtUserId.Text = userId.ToString();
bindCookies();
txtUser.Text = Response.Cookies["Person"][("Username")];
txtDepartment.Text = Response.Cookies["Person"]["Department"];
txtContact.Text = Response.Cookies["Person"]["Contact"];
txtMail.Text = Response.Cookies["Person"]["E-Mail"];
}
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="aspnet_cookies.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:Panel ID="Panel1" runat="server" BorderStyle="Double" Height="329px" Width="483px" BackColor="#33CCFF">
<br />
<asp:Label ID="Label5" runat="server" Font-Names="Arial" Font-Size="X-Large" Text="UserId :"></asp:Label>
<asp:TextBox ID="txtUserId" runat="server" Font-Names="Arial" Font-Size="X-Large" Height="20px" Width="158px"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label1" runat="server" Font-Names="Arial" Font-Size="X-Large" Text="Username :"></asp:Label>
<asp:TextBox ID="txtUser" runat="server" Font-Names="Arial" Font-Size="X-Large" Height="20px" Width="158px"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label2" runat="server" Font-Names="Arial" Font-Size="X-Large" Text="Department :"></asp:Label>
<asp:TextBox ID="txtDepartment" runat="server" Font-Names="Arial" Font-Size="X-Large" Height="20px" Width="158px"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label3" runat="server" Font-Names="Arial" Font-Size="X-Large" Text="Contact :"></asp:Label>
<asp:TextBox ID="txtContact" runat="server" Font-Names="Arial" Font-Size="X-Large" Height="20px" Width="158px"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label4" runat="server" Font-Names="Arial" Font-Size="X-Large" Text="E-Mail :"></asp:Label>
<asp:TextBox ID="txtMail" runat="server" Font-Names="Arial" Font-Size="X-Large" Height="20px" Width="296px"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnClick" runat="server" Font-Names="Arial" Font-Size="X-Large" Height="43px" OnClick="btnClick_Click" Text="Get" Width="85px" />
<asp:Button ID="btnPrevious" runat="server" Font-Names="Arial" Font-Size="X-Large" Height="43px" OnClick="btnPrevious_Click" Text="<" Width="85px" />
<asp:Button ID="btnNext" runat="server" Font-Names="Arial" Font-Size="X-Large" Height="43px" OnClick="btnNext_Click" Text=">" Width="85px" />
</asp:Panel>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN