Website geliştiricilerinin genellikle kullanıcılarına bazı özellikler ve sitelerine tekrar girdikleri zaman görmelerini istedikleri verileri saklamak için kullandıkları en önemli araçlardan biri de Cookilerdir. Şimdi beraber class kullanarak sitemizde istediğimiz zaman her yerde kullanıp kod kalabalığı yaratmadan cookiler üzerinde işlemler gerçekleştirecez.

Visual Studio üzerinden açtığımız Web projemize claasımızı ekleyelim

classımızın ismini belirttikten sonra koddlammamıza başlayabiliriz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Sf.CmsB2C.Utility.B2C
{
public static class CookieManager
{
//cookie oluşturmak ve veri eklemek için fonksiyonumuza başlayalım
public static void Write2Cookie(string cookieType, string cookieName, string cookieValue)
{
//cookie mizi oluşturduk
HttpCookie cookie;
int cookieCount = 0;// varsa cookimiz içerisindeki verimizi sayacak olan counter
if (System.Web.HttpContext.Current.Request.Cookies[cookieType] != null)
{
//cookie zaten var ise içinni okuyup count edilen değerden sonrasına
// verimi ekliyeceğim
cookieCount = System.Web.HttpContext.Current.Request.Cookies[cookieType].Values.Count;
cookie = System.Web.HttpContext.Current.Request.Cookies[cookieType];
cookie.Values[cookieName + cookieCount.ToString()] = cookieValue;
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}
if (System.Web.HttpContext.Current.Request.Cookies[cookieType] == null)
{
//cookie boş değer döndüğü için yeni bir cookie oluşturup verilerimi atıyorum
cookie = new HttpCookie(cookieType);
cookie.Values[cookieName + cookieCount.ToString()] = cookieValue;
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
}
}
public static void DeleteCookie(string cookieType)
{
System.Web.HttpContext.Current.Request.Cookies.Remove(cookieType);
}
/// <summary>
/// cookie ismi string halinde yollanır return değeri string List türündendir
/// ve cookie deki son kayıt list in ilk kaydı olacak şekilde ters sıralama ile return yapar
/// </summary>
/// <param name="cookieType"></param>
/// <returns></returns>
public static List<string> ReadCookie(string cookieType)
{
List<string> readedCookie =new List<string>();
if (System.Web.HttpContext.Current.Request.Cookies[cookieType] != null)
{
int count = System.Web.HttpContext.Current.Request.Cookies[cookieType].Values.Count;
for (int i = count-1; i >=0 ; i--)
{
readedCookie.Add(System.Web.HttpContext.Current.Request.Cookies[cookieType].Values[i]);
}
}
return readedCookie;
}
public static List<string> ReadCookie(string cookieType , int numberOfCookie)
{
List<string> readedCookie = new List<string>();
int difference = 0;
if (System.Web.HttpContext.Current.Request.Cookies[cookieType] != null)
{
int count = System.Web.HttpContext.Current.Request.Cookies[cookieType].Values.Count;
if (numberOfCookie <= count)
{
difference = count - numberOfCookie;
}
for (int i = count - 1; i >= difference ; i--)
{
readedCookie.Add(System.Web.HttpContext.Current.Request.Cookies[cookieType].Values[i]);
}
}
return readedCookie;
}
}
}