asp.net-web programming - sessions and cookies

18
Introduction to ASP.net Web programming Week 11, day2

Upload: baabtracom-no-1-supplier-of-quality-freshers

Post on 20-Jun-2015

412 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: ASP.NET-Web Programming - Sessions and Cookies

Introduction to ASP.netWeb programming

Week 11, day2

Page 2: ASP.NET-Web Programming - Sessions and Cookies

Cookies

Page 3: ASP.NET-Web Programming - Sessions and 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

Page 4: ASP.NET-Web Programming - Sessions and Cookies

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

Page 5: ASP.NET-Web Programming - Sessions and Cookies

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);

Page 6: ASP.NET-Web Programming - Sessions and Cookies

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)

Page 7: ASP.NET-Web Programming - Sessions and Cookies

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);

}

Page 8: ASP.NET-Web Programming - Sessions and Cookies

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

Page 9: ASP.NET-Web Programming - Sessions and Cookies

Sessions

Page 10: ASP.NET-Web Programming - Sessions and Cookies

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

Page 11: ASP.NET-Web Programming - Sessions and Cookies

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

Page 12: ASP.NET-Web Programming - Sessions and Cookies

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

Page 13: ASP.NET-Web Programming - Sessions and Cookies

Destroying a session variable

C#

Session.Remove("FirstName”);

Session.Remove("LastName“);

VB.net

SessionRemove ("FirstName")

SessionRemove ("LastName")

Page 14: ASP.NET-Web Programming - Sessions and Cookies

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

Page 15: ASP.NET-Web Programming - Sessions and Cookies

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

Page 16: ASP.NET-Web Programming - Sessions and Cookies

End of Day

Page 17: ASP.NET-Web Programming - Sessions and Cookies

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

Page 18: ASP.NET-Web Programming - Sessions and Cookies

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]