state management. what is state management why state management viewstate querystring cookies

39
State Management

Upload: gwenda-harvey

Post on 29-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: State Management. What is State management Why State management ViewState QueryString Cookies

State Management

Page 2: State Management. What is State management Why State management ViewState QueryString Cookies

What is State management Why State management ViewState QueryString Cookies

Page 3: State Management. What is State management Why State management ViewState QueryString Cookies

State management

In a window program the user interacts with the continuously running application

A portion of memory on the desktop computer is allocated to store the current set of working information

In Web application the story is different

Page 4: State Management. What is State management Why State management ViewState QueryString Cookies

A professional ASP.NET site might look like a continuously running application

Web application uses a disconnected access pattern

Page 5: State Management. What is State management Why State management ViewState QueryString Cookies

Why State Management-Disconnected access pattern

Client Web Server

Connect & request a page

Response & info abandoned

Web servers discard the client information

Page 6: State Management. What is State management Why State management ViewState QueryString Cookies

Web Application handles of thousands of requests

Web Server

Client 1

Client 2

Client 3

Client 4

Client 5

Client 6

Page 7: State Management. What is State management Why State management ViewState QueryString Cookies

The clients can be connected to the web server only for a few seconds

Web server needs to serve thousands of request To retain the working information the user needs to

take special measure

Page 8: State Management. What is State management Why State management ViewState QueryString Cookies

Viewstate QueryString Cookies

Page 9: State Management. What is State management Why State management ViewState QueryString Cookies

Viewstate

It allows the controls to retain their state Viewstate information is stored in the hidden field The hidden field holds the information in a crazy

string format that is non-readable and it changes dynamically everytime the informations in the control is changed

It is the first option for state management

Page 10: State Management. What is State management Why State management ViewState QueryString Cookies

ViewState Example :Counter

Dim count As Integer If Me.ViewState("count") = Nothing Then count = 1 Else count = CType(Me.ViewState("count"), Integer) + 1

End If Me.ViewState("count") = count Label1.Text = count.ToString()

Page 11: State Management. What is State management Why State management ViewState QueryString Cookies

Transferring information

One of the limitations of the viewstate is that it is tightly bound to a page

Page navigation makes the data to get lost One common approach that we use is to pass

information using the query string Used in search engines

http://www.askjeeves.com/main/askjeeves.asp?ask=organic

Page 12: State Management. What is State management Why State management ViewState QueryString Cookies

QueryString

The querystring is a portion of the URL followed after the ?

In the above example, it defines a single variable named ask which contains the string “organic”

It is a useful technique in the database applications where you present a list of details that correspond to records in the db

Page 13: State Management. What is State management Why State management ViewState QueryString Cookies

QueryString

The user can select an item and be forwarded to a detailed information page

This page can receive the unique ID that identifies the record from the sending page and look up for information from the db

Example Amazon site

Response.redirect(“newpage.aspx?recordID=10”)

Page 14: State Management. What is State management Why State management ViewState QueryString Cookies

Querystring example

Page 15: State Management. What is State management Why State management ViewState QueryString Cookies

Querystring example

if (ListBox1.SelectedIndex == -1) { Label1.Text = "U must select a item"; } else { string qs = "Default.aspx?"; qs += "Item=" + ListBox1.SelectedItem.Text; Response.Redirect(qs); }

Page 16: State Management. What is State management Why State management ViewState QueryString Cookies

QueryString

The new page can receive the values from the querystring with Request object

Dim ID as String=Request.QuerySting(“recordID”)

Page 17: State Management. What is State management Why State management ViewState QueryString Cookies

QueryString limitation

Information is limited to simple string; which contain legal URL characters

Information is clearly visible to the user (eavesdrop) Less security over the Internet Browsers impose restriction on the length of the

Querystring,hence a large string cannot be placed

Page 18: State Management. What is State management Why State management ViewState QueryString Cookies

Cookies

Custom cookies provide another way that you can store information

Cookies are small files that are stored on the hard drive

Cookies work transparently (ie) without the user being aware that information are stored

Long term storage

Page 19: State Management. What is State management Why State management ViewState QueryString Cookies

Drawbacks

Suffer with the limitation of the string length Information's are easily readable by the user and less

security

Page 20: State Management. What is State management Why State management ViewState QueryString Cookies

Using Cookies

Use the namespace Using System.Net Both the Request and Response objects provide the

cookie collection The cookies are retrieved from the Request object and

cookies are set using the Response object To set a cookie just create System.Net.HttpCookie

object

Page 21: State Management. What is State management Why State management ViewState QueryString Cookies

Creating cookies

Set a value in it

Add it to the cookies collection

Cookie=new HttpCookie(Preferences”)

Cookie(“LanguagePref”)=English

Response.cookies.add(Cookie)

Page 22: State Management. What is State management Why State management ViewState QueryString Cookies

Setting the expiry time for cookies A cookie normally persists until the user closes the

browser and will be sent with the request.To create a long lived cookie set the expiry date

This cookie lives for 1 year

Cookie.Expires =DateTime.Now.AddYears(1)

Page 23: State Management. What is State management Why State management ViewState QueryString Cookies

Check for the existence of cookies

Private Sub Page_Load(sender as Object,e as EventArgs){dim cookie As HttpCookie =Request.Cookies(“Preferences”)if cookie Is Nothing ThenLabelWelcome.Text=”Unkown Customer”elseLabelWelcomet.Text=”Cookie found”LabelWelcome.Text=”Cookie found,” & Cookie(“Name”)End IfEnd Sub

Page 24: State Management. What is State management Why State management ViewState QueryString Cookies

Cookies storing and retrieving

Private sub cmdstore_Click(sender as object,e as EventArgs)Dim cookie as HttpCookie=Request.Cookies(“Preferences”)If cookie is Nothing ThenCookie=new HttpCookie(“Preferences”)End If

Cookie(“Name”)=TextName.TextCookie.Expires=DateTime.AddYears(1)Response.Cookies.Add(Cookie)LabelWelcome.Text=”New customer” & Cookie(“Name”)

Page 25: State Management. What is State management Why State management ViewState QueryString Cookies

Session State

Application need to access complex information such as Datasets which cannot be persisted by Querystring or cookies

In these situations we use the ASP.NET built in facility Session state facility

It is one of the premiere feature It allows you to store any type of data on the server

Page 26: State Management. What is State management Why State management ViewState QueryString Cookies

Session State

The information is protected with a unique session.

Every client who access the application is created with a unique session

Page 27: State Management. What is State management Why State management ViewState QueryString Cookies

Session tracking

ASP.NET tracks each session using a 120 bit identifier.

It uses a special algorithm to track the value No hackers can guess the session ID This ID is the piece of information transferred

between the client and the server

Page 28: State Management. What is State management Why State management ViewState QueryString Cookies

Session Information I

Session IDSession Information II

Session ID

User presents the Session-ID

Matches the Session ID and

Returns the Session information

Page 29: State Management. What is State management Why State management ViewState QueryString Cookies

Session State -Storing values in Session State

Syntax

Session(“Session variable”) =Datasource

Example

Session(“ds)=dsinfo

Page 30: State Management. What is State management Why State management ViewState QueryString Cookies

Session State -Retrieving values from Session

Syntax

Temporary variable =Ctype(Session(“Session Variable”),DataSet)

Example

ds=CType(Session(“ds”),DataSet)

Page 31: State Management. What is State management Why State management ViewState QueryString Cookies

How can Session State be lost

If the user closes the browser If the session is timed out If the programmer ends the session in the

program If the user access the same page through

different browser

Page 32: State Management. What is State management Why State management ViewState QueryString Cookies

Example

Restrict only the signed in users to enter the appropriate page

Page 33: State Management. What is State management Why State management ViewState QueryString Cookies

Session State configuration

Session State is configured through Web.config file in the ASP.NET

It allows to set most of the options such as Session State mode and timeout ..

Page 34: State Management. What is State management Why State management ViewState QueryString Cookies

CookieLess

You can set the cookieless setting to true or false <sessionState>

cookieless=”false” </sessionState>

Page 35: State Management. What is State management Why State management ViewState QueryString Cookies

Session State

When set to true,the session ID will automatically be inserted into the URL

ASP.NET on receiving the request it collects the session information

Page 36: State Management. What is State management Why State management ViewState QueryString Cookies

Timeout

Another Session State settings in the web.config file is timeout

It specifes the number of minutes that ASP.NET will wait,without receiving the Request,before it discards the session

<SessionState> timout=”20” </SessionState>

Page 37: State Management. What is State management Why State management ViewState QueryString Cookies

Mode Off: this setting disables state management for

every Page in the application StateServer:Separate windows service outside

ASP.NET Set manual setting to start on and off

SqlServer:It stores the session information in the databases available in the SQL Server

Page 38: State Management. What is State management Why State management ViewState QueryString Cookies

Application State

It stores global objects that can be used by any client

A common example is the global counter that tracks how many times an operation has been performed by all clients

Page 39: State Management. What is State management Why State management ViewState QueryString Cookies

Global counter

Protected Sub Page_Load(sender as Object,e as EventArgs)Application .Lock( )Dim count as Integer =CType(Application(“Hit”,Integer))Count+=1Application(“Hit”)=countApplication.Unlock( )Label1.text=Count.ToString( )End Sub