asp.net-web programming - sessions and cookies
TRANSCRIPT
Introduction to ASP.netWeb programming
Week 11, day2
Cookies
Cookies
• HTTP is a stateless protocol; this means that the web server does not know
(or care) whether two requests comes from the same user or not; it just
handles each request without regard to the context in which it happens.
• Cookies are used to maintain the state in between requests—even when
they occur at large time intervals from each other.
• Cookies allow your applications to store a small amount of textual data
(typically,4-6kB) on a Web client browser.
• There are a number of possible uses for cookies, although their most
common one is maintaining state of a user
Creating cookieC#
• Response.Cookies*"cookie“+ = "cookie value";
Response.Cookies["cookie"].Expires = DateTime.Now.AddMinutes(10); //
add expiry time
VB.net
• Response.Cookies("cookie“) = "cookie value”
Response.Cookies("cookie“).Expires = DateTime.Now.AddMinutes(10) //
add expiry time
• This simply sets a cookie variable named “cookie” with value
“cookie value” and this variable value will be available till next 10
minutes from current time
Accessing CookiesC#
• Request.Cookies*"cookie“+ = "cookie value";
VB.net
• Request.Cookies("cookie“) = "cookie value";
Cookie as array
C#
• Response.Cookies["UserSettings"]["Font"] = "Arial";
• Response.Cookies["UserSettings"]["Color"] = "Blue";
• Response.Cookies["UserSettings"].Expires = DateTime.Now.AddDays(1d);
Cookie as arrayCookie as array
VB.net
• Response.Cookies("UserSettings")("Font") = "Arial"
• Response.Cookies("UserSettings")("Color") = "Blue"
• Response.Cookies("UserSettings").Expires = DateTime.Now.AddDays(1)
Destroying Cookies• You cannot directly delete a cookie on a user's computer. However, you can
direct the user's browser to delete the cookie by setting the cookie's expiration
date to a past date. The next time a user makes a request to a page within the
domain or path that set the cookie, the browser will determine that the cookie
has expired and remove it.
• C#
if (Request.Cookies["UserSettings"] != null)
{
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
Destroying Cookies
• VB.net
If (Not Request.Cookies("UserSettings") Is Nothing) Then
Dim myCookie As HttpCookie
myCookie = New HttpCookie("UserSettings")
myCookie.Expires = DateTime.Now.AddDays(-1D)
Response.Cookies.Add(myCookie)
End If
Sessions
Sessions• Session serve the same purpose of cookies that is sessions are used
to maintain the state in between requests
• The difference of session variables with cookies is that they are
stored in the server while cookie variables are stored on the client
(browser)
• In asp.net a session gets started when the user starts interacting
with the server, that is when the user first accesses a page from the
application
• Session can be used to store values as session variables which will be
available throughout the session
Sessions• Session can support any type of object to store along with our own custom
objects
• For every client, session data is stored separately, which means session data is
stored on a per client basis
Creating session variables
C#
Session["FirstName"] = FirstNameTextBox.Text;
Session["LastName"] = LastNameTextBox.Text;
VB.net
Session("FirstName") = FirstNameTextBox.Text
Session("LastName") = LastNameTextBox.Text
Any .net framework object type can be stored in a session variable
Destroying a session variable
C#
Session.Remove("FirstName”);
Session.Remove("LastName“);
VB.net
SessionRemove ("FirstName")
SessionRemove ("LastName")
Destroying the session itself
C#
Session.clear(); //clears all the session variables
Session.Abandon(); //destroys the whole session
VB.net
Session.clear(); //clears all the session variables
Session.Abandon(); //destroys the whole session
Comparison
cookies are stored in the user's
browser
A cookie can keep information in the
user's browser until deleted by user or
set as per the timer. It will not be
destroyed even if you close the browser.
Cookies can only store string
we can save cookie for future
reference
Sessions are stored in server
A session is available as long as
the browser is opened. User cant
disable the session. It will be
destroyed if you close the browser
Can store any object
session can’t be.
Cookies Session
End of Day
If this presentation helped you, please visit our page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Start up VillageEranakulam,Kerala, India.
Email: [email protected]