Asp.net Cookie (Çerez) Oluşturma ve Okuma

Asp.net ile kullanıcının girmiş olduğu bilgileri cookie olarak ayarlayabilirsiniz kullanıcı adı, şifre.. gibi cookie ayarlamak için

 HttpCookie cookieornek = Request.Cookies["cookieornek"];  
        cookieornek["isim"] = TextBox1.Text.ToString();  
        cookieornek["sehir"] = TextBox2.Text.ToString();  
        Response.Cookies.Add(cookieornek);  

komutlarını, ayarlanan cookie bilgilerini okumak için

HttpCookie cookieornek = Request.Cookies["cookieornek"];  
        if (cookieornek != null) {  
            string isim = cookieornek["isim"];  
            string sehir = cookieornek["sehir"];  
  
            label1.Text = "Cookie bulundu<br />";  
            label1.Text += "Adınız: " + isim + "<br />Bulunduğunuz İl: " + sehir;  
        }  

komutlarını kullanıyoruz ilk olarak birinci butona basılınca textboxlara girilen bilgiler cookiornek değişkenindeki ile tanımlanana isim ve sehir cookie değerlerine atanıyor ve Response.Cookies.Add ile oluşturuluyor , ikinci butona basılınca cookie olup olmadığı kontrol ediliyor eğer var ise içindeki bilgiler Request.Cookies ile okunup yazdırılıyor.
cookie4

<%@ Page Language="C#" %>  
<%@ Import Namespace="System.Net" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<script runat="server">  
    protected void Page_Load(object sender, System.EventArgs e) {  
        if (!this.IsPostBack) {  
            HttpCookie bilgisakla = new HttpCookie("cookieornek");  
            bilgisakla.Expires = DateTime.Now.AddDays(5);  
            Response.Cookies.Add(bilgisakla);  
        }  
    }  
  
    protected void Button1_Click(object sender, System.EventArgs e) {  
        HttpCookie cookieornek = Request.Cookies["cookieornek"];  
        cookieornek["isim"] = TextBox1.Text.ToString();  
        cookieornek["sehir"] = TextBox2.Text.ToString();  
        Response.Cookies.Add(cookieornek);  
    }  
  
    protected void Button2_Click(object sender, System.EventArgs e) {  
        HttpCookie cookieornek = Request.Cookies["cookieornek"];  
        if (cookieornek != null) {  
            string isim = cookieornek["isim"];  
            string sehir = cookieornek["sehir"];  
  
            label1.Text = "Cookie bulundu<br />";  
            label1.Text += "Adınız: " + isim + "<br />Bulunduğunuz İl: " + sehir;  
        }  
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>asp.net cookie örnekleri: www.aspnetornekleri.com</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:Red">asp.net cookie örnekleri: cookie değer atama</h2>  
        <asp:Label  ID="label1"   
            runat="server"  Font-Size="Large" ForeColor="SeaGreen"  Font-Italic="true"  >  
        </asp:Label>  
        <br /><br />  
        <asp:Label ID="Label2" runat="server" Text="Adınız">  
        </asp:Label>  
        <asp:TextBox ID="TextBox1" runat="server" BackColor="LightGreen">  
        </asp:TextBox>  
        <br />  
        <asp:Label ID="Label3" runat="server" Text="Yaşadığınız İl">  
        </asp:Label>  
        <asp:TextBox ID="TextBox2" runat="server" BackColor="LightGreen">  
        </asp:TextBox>  
        <br />  
        <asp:Button    ID="Button1"    runat="server"    Text="Cookie Ayarla"   
            OnClick="Button1_Click"   Font-Bold="true"   ForeColor="Crimson"   />  
        <asp:Button    ID="Button2"   runat="server"   
            Text="Cookie Oku"    OnClick="Button2_Click"   Font-Bold="true"   ForeColor="DarkGreen"   />  
    </div>  
    </form>  
</body>  
</html>  

You may also like...

Subscribe
Bildir
guest
1 Yorum
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
qwe

yardım

1
0
Yazı hakkındaki yorum, görüş ve önerilerinizi yazınx
()
x