SESSIONS & COOKIES
Are state management techniques (used to maintain the state of the application
since http is a stateless protocol).
Basic about State Management in ASP.NET
• ASP.NET framework provides various ways to preserve
states at various stage.
• Controlstate, viewstate, sessions, cookies etc.
• This can be categorised as client side and server side
state management techniques.
SESSIONS
• Session stores variables on the Server Side.
• Session will allocates memory in the web server and for every
user server will allocates memory, so Session is per user
based.
• Sessions are generally used to maintain state when you
navigate through a website. However, they can also be used to
hold commonly accessed objects.
• Every session is identified by a unique session id.
• Sessions are not reliant on the user allowing a cookie. They
work instead like a token allowing access and passing
information while the user has their browser open.
• The problem with sessions is that when you close your
browser you also lose the session. So, if you had a site
requiring a login, this couldn't be saved as a session like it
could as a cookie, and the user would be forced to re-login
every time they visit.
EXAMPLE:
Session["Name"] = “textName”;
Response.Write(Session[“Name"]);
Code Explanation:-
• The first line of code takes the value of the Name textbox
control and stores it in the Session object. By specifying the
code of Session["Name"] , we are giving the property a name
called "Name." By specifying a name for the property, it
becomes easier to retrieve it at a later point in time.
• The next line of code retrieves the stored value from the
Session object. It then writes this value via the
'Response.Write' method back to the client.
ADVANTAGES AND DISADVANTAGES
• Advantages:
 Session provide us a way to maintain user data, all over
the applications.
 We can store any type of object in it.
 Session is secure and transparent from the user.
• Disadvantages:
 Performance overhead in case of large volumes of user,
because data is stored in server memory.
COOKIES
• Basically a cookie is Client Side variable and will store data either in browser or in
system hard disk.
• Cookies are stored per-user on the users machine. A cookie is usually just a bit of
information.
• You can never fully trust that a cookie has not been tampered with by a user or
outside source however if security is a big concern and you must use cookies then
you can either encrypt your cookies or set them to only be transmitted.
• A user can clear there cookies at any time or not allow cookies altogether so you
cannot count on them being there just because a user has visited your site in the
past.
• A cookie can keep information in the user's browser until deleted. If a person has a
login and password, this can be set as a cookie in their browser so they do not have
to re-login to your website every time they visit.
• You can store almost anything in a browser cookie.
• The trouble is that a user can block cookies or delete them at any time. If, for
example, your website's shopping cart utilized cookies, and a person had their
browser set to block them, then they could not shop at your website.
EXAMPLE:
<%
Response.Cookies("firstname")=“mlsu”;
Response.Cookies("firstname").Expires=#May 10,2019#;
%>
Code Explanation:-
• Here we will create a cookie named "firstname" and assign the value
“mlsu" to it. And in the second line we are setting a date, when the
cookie should expire.
<%
fname=Request.Cookies("firstname");
Response.Write("Firstname=" & fname);
%>
Code Explanation:-
• We retrieve the value of the cookie named "firstname" and display it
on a page.
ADVANTAGES AND DISADVANTAGES
• Advantages:
 It is very simple to use and implement.
 Browser is in-charge of sending data.
 For multiple sites with cookies, the browser automatically
arranges them.
• Disadvantages:
 It stores data in simple text format, so it’s not secure at all.
There is a size limit for cookies data(4KB).
Most browsers provide limits for number of cookies at a time
which is 20.
DIFFERENCE BETWEEN COOKIES AND
SESSION
Note: You can of course get the best of both worlds! Once you know
what each does, you can use a combination of cookies and sessions to
make your site work exactly the way you want it to.
1. Client-side state management
technique, stored in on client's
browser.
2. Can store text data.
3. Not suggestible for storing
critical information.
4. Cookie doesn't have a self-
expiry time.
1. Server-side state management
technique, stored in server.
2. Can stored an object.
3. Can be used for storing critical
information.
4. Session expires after 20
minutes by default.
Cookies Sessions
THANK YOU

More Related Content

PPTX
Cookies in servlets.ppt
PPTX
Protect Your System - Use Browser Junk Removal Tool
PPTX
APEX & Cookie Monster
PPTX
Using cookies and sessions
ODP
Caching idea for midcom
PPTX
Cookies authentication
PPTX
State Management.pptx
Cookies in servlets.ppt
Protect Your System - Use Browser Junk Removal Tool
APEX & Cookie Monster
Using cookies and sessions
Caching idea for midcom
Cookies authentication
State Management.pptx

Similar to Sessions&cookies (20)

PPTX
Enterprise java unit-2_chapter-2
PPTX
Enterprise java unit-2_chapter-3
PPTX
Authentication in Svelte using cookies.pptx
PDF
Session and Cookies.pdf
PPT
Cookies in servlet
PPTX
Session and cookies,get and post
PDF
E Commerce Analytics Demandware
PPT
session and cookies.ppt
PPTX
Cookies and Session
PDF
A Comprehensive Guide to Cookie Management Using HeadSpin's Cutting-Edge Remo...
PPTX
Cookies testing
PPTX
Backend Technologies Notes ajef;asnfkndfdsa
PPTX
Cookies: HTTP state management mechanism
PPTX
PPTX
PPTX
C# cookieless session id and application state
PPTX
Cookie & Session In ASP.NET
PPTX
PPSX
Sessions and cookies
PPT
PHP - Introduction to PHP Cookies and Sessions
Enterprise java unit-2_chapter-2
Enterprise java unit-2_chapter-3
Authentication in Svelte using cookies.pptx
Session and Cookies.pdf
Cookies in servlet
Session and cookies,get and post
E Commerce Analytics Demandware
session and cookies.ppt
Cookies and Session
A Comprehensive Guide to Cookie Management Using HeadSpin's Cutting-Edge Remo...
Cookies testing
Backend Technologies Notes ajef;asnfkndfdsa
Cookies: HTTP state management mechanism
C# cookieless session id and application state
Cookie & Session In ASP.NET
Sessions and cookies
PHP - Introduction to PHP Cookies and Sessions
Ad

Recently uploaded (20)

PPTX
Top Website Bugs That Hurt User Experience – And How Expert Web Design Fixes
PDF
Exploring VPS Hosting Trends for SMBs in 2025
PPTX
artificialintelligenceai1-copy-210604123353.pptx
PDF
📍 LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1 TERPOPULER DI INDONESIA ! 🌟
PPTX
IPCNA VIRTUAL CLASSES INTERMEDIATE 6 PROJECT.pptx
PDF
Understand the Gitlab_presentation_task.pdf
PPTX
1402_iCSC_-_RESTful_Web_APIs_--_Josef_Hammer.pptx
PDF
📍 LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1 TERPOPULER DI INDONESIA ! 🌟
PDF
BIOCHEM CH2 OVERVIEW OF MICROBIOLOGY.pdf
PPTX
Internet Safety for Seniors presentation
PDF
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
PDF
SlidesGDGoCxRAIS about Google Dialogflow and NotebookLM.pdf
PDF
Alethe Consulting Corporate Profile and Solution Aproach
PDF
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
PPTX
Introduction to cybersecurity and digital nettiquette
PPTX
TITLE DEFENSE entitle the impact of social media on education
PPT
415456121-Jiwratrwecdtwfdsfwgdwedvwe dbwsdjsadca-EVN.ppt
PPTX
curriculumandpedagogyinearlychildhoodcurriculum-171021103104 - Copy.pptx
PDF
simpleintnettestmetiaerl for the simple testint
PDF
Course Overview and Agenda cloud security
Top Website Bugs That Hurt User Experience – And How Expert Web Design Fixes
Exploring VPS Hosting Trends for SMBs in 2025
artificialintelligenceai1-copy-210604123353.pptx
📍 LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1 TERPOPULER DI INDONESIA ! 🌟
IPCNA VIRTUAL CLASSES INTERMEDIATE 6 PROJECT.pptx
Understand the Gitlab_presentation_task.pdf
1402_iCSC_-_RESTful_Web_APIs_--_Josef_Hammer.pptx
📍 LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1 TERPOPULER DI INDONESIA ! 🌟
BIOCHEM CH2 OVERVIEW OF MICROBIOLOGY.pdf
Internet Safety for Seniors presentation
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
SlidesGDGoCxRAIS about Google Dialogflow and NotebookLM.pdf
Alethe Consulting Corporate Profile and Solution Aproach
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
Introduction to cybersecurity and digital nettiquette
TITLE DEFENSE entitle the impact of social media on education
415456121-Jiwratrwecdtwfdsfwgdwedvwe dbwsdjsadca-EVN.ppt
curriculumandpedagogyinearlychildhoodcurriculum-171021103104 - Copy.pptx
simpleintnettestmetiaerl for the simple testint
Course Overview and Agenda cloud security
Ad

Sessions&cookies

  • 1. SESSIONS & COOKIES Are state management techniques (used to maintain the state of the application since http is a stateless protocol).
  • 2. Basic about State Management in ASP.NET • ASP.NET framework provides various ways to preserve states at various stage. • Controlstate, viewstate, sessions, cookies etc. • This can be categorised as client side and server side state management techniques.
  • 3. SESSIONS • Session stores variables on the Server Side. • Session will allocates memory in the web server and for every user server will allocates memory, so Session is per user based. • Sessions are generally used to maintain state when you navigate through a website. However, they can also be used to hold commonly accessed objects. • Every session is identified by a unique session id. • Sessions are not reliant on the user allowing a cookie. They work instead like a token allowing access and passing information while the user has their browser open. • The problem with sessions is that when you close your browser you also lose the session. So, if you had a site requiring a login, this couldn't be saved as a session like it could as a cookie, and the user would be forced to re-login every time they visit.
  • 4. EXAMPLE: Session["Name"] = “textName”; Response.Write(Session[“Name"]); Code Explanation:- • The first line of code takes the value of the Name textbox control and stores it in the Session object. By specifying the code of Session["Name"] , we are giving the property a name called "Name." By specifying a name for the property, it becomes easier to retrieve it at a later point in time. • The next line of code retrieves the stored value from the Session object. It then writes this value via the 'Response.Write' method back to the client.
  • 5. ADVANTAGES AND DISADVANTAGES • Advantages:  Session provide us a way to maintain user data, all over the applications.  We can store any type of object in it.  Session is secure and transparent from the user. • Disadvantages:  Performance overhead in case of large volumes of user, because data is stored in server memory.
  • 6. COOKIES • Basically a cookie is Client Side variable and will store data either in browser or in system hard disk. • Cookies are stored per-user on the users machine. A cookie is usually just a bit of information. • You can never fully trust that a cookie has not been tampered with by a user or outside source however if security is a big concern and you must use cookies then you can either encrypt your cookies or set them to only be transmitted. • A user can clear there cookies at any time or not allow cookies altogether so you cannot count on them being there just because a user has visited your site in the past. • A cookie can keep information in the user's browser until deleted. If a person has a login and password, this can be set as a cookie in their browser so they do not have to re-login to your website every time they visit. • You can store almost anything in a browser cookie. • The trouble is that a user can block cookies or delete them at any time. If, for example, your website's shopping cart utilized cookies, and a person had their browser set to block them, then they could not shop at your website.
  • 7. EXAMPLE: <% Response.Cookies("firstname")=“mlsu”; Response.Cookies("firstname").Expires=#May 10,2019#; %> Code Explanation:- • Here we will create a cookie named "firstname" and assign the value “mlsu" to it. And in the second line we are setting a date, when the cookie should expire. <% fname=Request.Cookies("firstname"); Response.Write("Firstname=" & fname); %> Code Explanation:- • We retrieve the value of the cookie named "firstname" and display it on a page.
  • 8. ADVANTAGES AND DISADVANTAGES • Advantages:  It is very simple to use and implement.  Browser is in-charge of sending data.  For multiple sites with cookies, the browser automatically arranges them. • Disadvantages:  It stores data in simple text format, so it’s not secure at all. There is a size limit for cookies data(4KB). Most browsers provide limits for number of cookies at a time which is 20.
  • 9. DIFFERENCE BETWEEN COOKIES AND SESSION Note: You can of course get the best of both worlds! Once you know what each does, you can use a combination of cookies and sessions to make your site work exactly the way you want it to. 1. Client-side state management technique, stored in on client's browser. 2. Can store text data. 3. Not suggestible for storing critical information. 4. Cookie doesn't have a self- expiry time. 1. Server-side state management technique, stored in server. 2. Can stored an object. 3. Can be used for storing critical information. 4. Session expires after 20 minutes by default. Cookies Sessions

Editor's Notes

  • #10: the main difference between cookies and sessions is that cookies are stored in the user's browser, and sessions are not. This difference determines what each is best used for.