VB.net Tutorials with Examples

VB.Net Projects

VB.Net Project 1

adplus-dvertising
Cookies in ASP.NET
Previous Home Next

A cookie is a small bit of text file that browser creates and stores on your machine (hard drive). Cookie is a small piece of information stored as a string. Web server sends the cookie and browser stores it, next time server returns that cookie .Cookies are mostly used to store the information about the user. Cookies are stores on the client side.To store data in a cookie is not secure due to it's location at client end. Cookie was introduced with first version of Netscape navigator (that was 1.0).

Advantages

  1. Cookies do not require any server resources since they are stored on the client.
  2. Cookies are easy to implement.
  3. You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).

Disadvantages

  1. Users can delete a cookies.
  2. Users browser can refuse cookies ,so your code has to anticipate that possibility.
  3. Cookies exist as plain text on the client machine and they may pose a possible security risk as anyone can open and tamper with cookies.

ASPX CODE

<%@ Page 
Language="VB" 
AutoEventWireup="true" 
CodeFile="Default3.aspx.vb" 
Inherits="Default3"
 %>
<html xmlns=
"http://www.w3.org/1999/xhtml" > 
<head id=
"Head1" runat=
"server">
<title></title>
</head> 
<body> 
<form id=
"form1" runat=
"server">
<div> 
<fieldset>
<legend>rdfdf</legend>
<asp:TextBox ID=
"TextBox1" runat="server"
></asp:TextBox>
<asp:Button ID=
"Button1" runat="server" 
Text="Add" Width=
"70px" onclick="Button1_Click" />

<asp:Button ID=
"Button2" runat="server" Text
="View" Width=
"84px" onclick=
"Button2_Click" />
<br />
<br />
<asp:Label ID=
"Label1" runat=
"server" Text=
"" Width=
"138px"></asp:
Label>
</fieldset>
</div>
</form>
</body>
</html> 

CS CODE

Partial Class _Default
    Inherits System.
	Web.UI
	.Page
    Protected Sub Button1_Click(ByVal sender As Object
	, 
	ByVal e As System.
	EventArgs) Handles Button1.Click
    Response.Cookies("MyCookie"
	)("Data") = TextBox1.
	Text
    ' To Create  Cookies
    Response.Cookies("MyCookie"
	)("Time") = DateTime.Now.ToString
	("G")
    Response.Cookies("MyCookie").Expires = DateTime.Now.AddMonths(1)
    ' For Expire Cookies
    Label1.Text = ((
	"Cookie created!<p>" & 
	"Your cookie contains:<font color=red>")
 + Request.Cookies("MyCookie"
 )("Data") 
 & "<br>") + Request.Cookies
 ("MyCookie")(
 "Time") & "</font>"
    End Sub
    Protected Sub Button2_Click(ByVal sender As Object, 
	ByVal e As System.
	EventArgs) Handles Button2.Click
    If Request.Cookies(
	"MyCookie") Is Nothing Then
    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>"
   End If
   End Sub
   End Class
Previous Home Next