SlideShare a Scribd company logo
2
Most read
4
Most read
Introduction to ASP.net
Web 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 cookie
C#
• 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 Cookies
C#
• 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 array
Cookie 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 Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

More Related Content

What's hot (20)

PPTX
PHP Cookies and Sessions
Nisa Soomro
 
ODP
Introduction of Html/css/js
Knoldus Inc.
 
PPTX
Ajax
Tech_MX
 
PPTX
Introduction to CSS
Folasade Adedeji
 
PPTX
HTML History
TolulopeOkutoro
 
PDF
CSS3 Media Queries
Russ Weakley
 
PDF
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
PPT
JavaScript - An Introduction
Manvendra Singh
 
PPT
Active server pages
mcatahir947
 
PDF
Introduction to MongoDB
Mike Dirolf
 
PPT
JavaScript JQUERY AJAX
Makarand Bhatambarekar
 
PPTX
File system node js
monikadeshmane
 
PPT
ASP.NET Session 11 12
Sisir Ghosh
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PPT
Php Presentation
Manish Bothra
 
PPTX
Web design - Working with forms in HTML
Mustafa Kamel Mohammadi
 
PPSX
Javascript variables and datatypes
Varun C M
 
PPT
CSS Basics
WordPress Memphis
 
PPT
Web Servers (ppt)
webhostingguy
 
PHP Cookies and Sessions
Nisa Soomro
 
Introduction of Html/css/js
Knoldus Inc.
 
Ajax
Tech_MX
 
Introduction to CSS
Folasade Adedeji
 
HTML History
TolulopeOkutoro
 
CSS3 Media Queries
Russ Weakley
 
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
JavaScript - An Introduction
Manvendra Singh
 
Active server pages
mcatahir947
 
Introduction to MongoDB
Mike Dirolf
 
JavaScript JQUERY AJAX
Makarand Bhatambarekar
 
File system node js
monikadeshmane
 
ASP.NET Session 11 12
Sisir Ghosh
 
jQuery for beginners
Arulmurugan Rajaraman
 
Php Presentation
Manish Bothra
 
Web design - Working with forms in HTML
Mustafa Kamel Mohammadi
 
Javascript variables and datatypes
Varun C M
 
CSS Basics
WordPress Memphis
 
Web Servers (ppt)
webhostingguy
 

Viewers also liked (7)

PPT
Chapter - 5 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
error007
 
PPT
Chapter - 6 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
error007
 
PPT
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Salah Amean
 
PDF
Lecture13 - Association Rules
Albert Orriols-Puig
 
PPT
Cookies and sessions
Lena Petsenchuk
 
PDF
Data Mining: Association Rules Basics
Benazir Income Support Program (BISP)
 
Chapter - 5 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
error007
 
Chapter - 6 Data Mining Concepts and Techniques 2nd Ed slides Han & Kamber
error007
 
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Salah Amean
 
Lecture13 - Association Rules
Albert Orriols-Puig
 
Cookies and sessions
Lena Petsenchuk
 
Data Mining: Association Rules Basics
Benazir Income Support Program (BISP)
 
Ad

Similar to ASP.NET-Web Programming - Sessions and Cookies (20)

PPT
Session and cookies,get and post methods
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
19_JavaScript - Storage_Cookies-tutorial .pptx
ssuser4a97d3
 
PDF
state management asp.net
Pratiksha Srivastava
 
PPTX
Sessions&cookies
Tirthika Bandi
 
PPSX
Sessions and cookies
www.netgains.org
 
PPT
State management
Lalit Kale
 
PPTX
Cookie testing
BugRaptors
 
PPTX
Session and cookies,get and post
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 8 part1
application developer
 
PDF
Session and Cookies.pdf
HamnaGhani1
 
PPT
Session,cookies
rkmourya511
 
PPT
Synapse india dotnet development web approch part 2
Synapseindiappsdevelopment
 
PPT
Session and state management
Paneliya Prince
 
PPT
2310 b 14
Krazy Koder
 
PDF
Web app development_cookies_sessions_14
Hassen Poreya
 
PDF
Introduction to php web programming - sessions and cookies
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Module-5_WTA_Managing State & jQuery
SIVAKUMAR V
 
PPTX
State Management.pptx
DrMonikaPatel2
 
PPTX
FP512 Cookies sessions
Fatin Fatihayah
 
PPTX
19_JavaScript - Storage_Cookies_students.pptx
VatsalJain39
 
Session and cookies,get and post methods
baabtra.com - No. 1 supplier of quality freshers
 
19_JavaScript - Storage_Cookies-tutorial .pptx
ssuser4a97d3
 
state management asp.net
Pratiksha Srivastava
 
Sessions&cookies
Tirthika Bandi
 
Sessions and cookies
www.netgains.org
 
State management
Lalit Kale
 
Cookie testing
BugRaptors
 
Session and cookies,get and post
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 8 part1
application developer
 
Session and Cookies.pdf
HamnaGhani1
 
Session,cookies
rkmourya511
 
Synapse india dotnet development web approch part 2
Synapseindiappsdevelopment
 
Session and state management
Paneliya Prince
 
2310 b 14
Krazy Koder
 
Web app development_cookies_sessions_14
Hassen Poreya
 
Introduction to php web programming - sessions and cookies
baabtra.com - No. 1 supplier of quality freshers
 
Module-5_WTA_Managing State & jQuery
SIVAKUMAR V
 
State Management.pptx
DrMonikaPatel2
 
FP512 Cookies sessions
Fatin Fatihayah
 
19_JavaScript - Storage_Cookies_students.pptx
VatsalJain39
 
Ad

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
PDF
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 3 stored procedures
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 

Recently uploaded (20)

PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
[GDGoC FPTU] Spring 2025 Summary Slidess
minhtrietgect
 
PDF
Home Cleaning App Development Services.pdf
V3cube
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
PDF
NASA A Researcher’s Guide to International Space Station : Earth Observations
Dr. PANKAJ DHUSSA
 
PDF
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Next Generation AI: Anticipatory Intelligence, Forecasting Inflection Points ...
dleka294658677
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
[GDGoC FPTU] Spring 2025 Summary Slidess
minhtrietgect
 
Home Cleaning App Development Services.pdf
V3cube
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
NASA A Researcher’s Guide to International Space Station : Earth Observations
Dr. PANKAJ DHUSSA
 
Survival Models: Proper Scoring Rule and Stochastic Optimization with Competi...
Paris Women in Machine Learning and Data Science
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Next Generation AI: Anticipatory Intelligence, Forecasting Inflection Points ...
dleka294658677
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 

ASP.NET-Web Programming - Sessions and Cookies

  • 1. Introduction to ASP.net Web programming Week 11, day2
  • 3. 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
  • 4. Creating cookie C# • 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
  • 5. Accessing Cookies C# • 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);
  • 6. Cookie as array Cookie as array VB.net • Response.Cookies("UserSettings")("Font") = "Arial" • Response.Cookies("UserSettings")("Color") = "Blue" • Response.Cookies("UserSettings").Expires = DateTime.Now.AddDays(1)
  • 7. 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); }
  • 8. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. Destroying a session variable C# Session.Remove("FirstName”); Session.Remove("LastName“); VB.net SessionRemove ("FirstName") SessionRemove ("LastName")
  • 14. 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
  • 15. 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
  • 17. 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
  • 18. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: [email protected]