ASP.NET-Web Programming - Sessions and Cookies


Published on

Published in: Technology, Self Improvement
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

ASP.NET-Web Programming - Sessions and Cookies

  1. 1. Introduction to Web programming Week 11, day2
  2. 2. Cookies
  3. 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. 4. Creating cookie C# • Response.Cookies*"cookie“+ = "cookie value"; Response.Cookies["cookie"].Expires = DateTime.Now.AddMinutes(10); // add expiry time • 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. 5. Accessing Cookies C# • Request.Cookies*"cookie“+ = "cookie value"; • 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. 6. Cookie as array Cookie as array • Response.Cookies("UserSettings")("Font") = "Arial" • Response.Cookies("UserSettings")("Color") = "Blue" • Response.Cookies("UserSettings").Expires = DateTime.Now.AddDays(1)
  7. 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. 8. Destroying Cookies • 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
  9. 9. Sessions
  10. 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 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. 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. 12. Creating session variables C# Session["FirstName"] = FirstNameTextBox.Text; Session["LastName"] = LastNameTextBox.Text; Session("FirstName") = FirstNameTextBox.Text Session("LastName") = LastNameTextBox.Text Any .net framework object type can be stored in a session variable
  13. 13. Destroying a session variable C# Session.Remove("FirstName”); Session.Remove("LastName“); SessionRemove ("FirstName") SessionRemove ("LastName")
  14. 14. Destroying the session itself C# Session.clear(); //clears all the session variables Session.Abandon(); //destroys the whole session Session.clear(); //clears all the session variables Session.Abandon(); //destroys the whole session
  15. 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
  16. 16. End of Day
  17. 17. If this presentation helped you, please visit our page and like it. Thanks in advance. | |
  18. 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: