SlideShare uma empresa Scribd logo
1 de 31
State Management
MUHAMMAD AMIR – A M I R 4IT@ GMAI L.COM
Why we need?
Stateless HTTP Protocol:
 No information about the request
 Request created/destroyed with each post backversion.
State Management in ASP.Net
State Management

Client Side!
Client Side – View State
 Mechanism that allows state values to be preserved across page postbacks
 EnableViewState property will be set to true

Advantages
 No server resources are required
 Simple implementation
 Enhanced security features

Disadvantages
 Performance considerations
 Device limitations
 Potential security risks
Client Side – View State
 Sample:

//To Save Information in View State
ViewState.Add ("NickName", "Dolly");
//Retrieving View state
String strNickName = ViewState ["NickName"];

 Code Example:
public int SomeInteger {
get {
object o = ViewState["SomeInteger"];
if (o != null) return (int)o;
return 0;
//a default
}
set { ViewState["SomeInteger"] = value; }
}

Code Sample
Client Side – View State
 Controls that use the view state
Client Side – Control State
 Essential for control to function properly
 It is Private viewstate for the control only
 Work even when ViewState turned off
Client Side – Control State

Code Sample

public class ControlStateWebControl : Control
{
private string _strStateToSave;
protected override void OnInit(EventArgs e)
{
Page.RegisterRequiresControlState(this);
base.OnInit(e);
}
protected override object SaveControlState() { return _strStateToSave; }
protected override void LoadControlState(object state)
{
if (state != null)
{
_strStateToSave = state.ToString();
}
}
}
Client Side – Hidden Fields
 Not rendered in browser
 Use to store small frequently changed data
Client Side – Hidden Fields

Code Sample

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)|
Label1.Text = string.Format("Clicked {0} times", HiddenField1.Value);
}
protected void Button1_Click(object sender, EventArgs e)
{
HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value) + 1).ToString();

}

Label1.Text = string.Format("Clicked {0} times", HiddenField1.Value);
Client Side – Cookies
 Stored at client side in text file or in memory of the client browser session
 Always sent with the request to the web server
 Read from user machine to identify user
Client Side – Cookies

Code Sample

//Storing value in cookie
HttpCookie cookie = new HttpCookie("NickName");
cookie.Value = "David";
Request.Cookies.Add(cookie);
//Retrieving value in cookie
if (Request.Cookies.Count > 0 && Request.Cookies["NickName"] != null)
lblNickName.Text = "Welcome" + Request.Cookies["NickName"].ToString();
else
lblNickName.Text = "Welcome Guest";
Client Side – Query String
 Used to pass information across pages
 Information passed along with URL in clear text
Client Side – Query String
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string v = Request.QueryString["param"];
if (v != null)
{
Response.Write("param is ");
Response.Write(v);
}
string x = Request.QueryString["id"];
if (x != null)
{
Response.Write(" id detected");
}
}
}

Code Sample
Client Side – Summary
Client Side – Summary
State management

Recommended usage

View state

Use when you need to store small amounts of information for a page that will post back to itself. Using the ViewState
property provides functionality with basic security.

Control state

Hidden fields

Cookies

Use when you need to store small amounts of state information for a control between round trips to the server.
Use when you need to store small amounts of information for a page that will post back to itself or to another page,
and when security is not an issue.

Note
You can use a hidden field only on pages that are submitted to the server.
Use when you need to store small amounts of information on the client and security is not an issue.
Use when you are transferring small amounts of information from one page to another and security is not an issue.

Query string

Note
You can use query strings only if you are requesting the same page, or another page via a link.
State Management

Server Side!
State Management in ASP.Net
Server Side – Application State
 Used to share information among users
 Stored in the memory of the windows process
 Great place to store data that changes infrequently
Server Side – Application State
protected void Page_Load(object sender, EventArgs e)
{
int count = 0;
if (Application["HitCounterForOrderPage"] != null)
count = (int)Application["HitCounterForOrderPage"];
count++;
Application["HitCounterForOrderPage"] = count;
lblcounter.Text = "Page Visited: " + count.ToString() + "Times";
}

//Application.Lock(); and Application.UnLock();

Code Sample
Server Side – Application State
//Stroing information in application state
lock (this)
{
Application["NickName"] = "Nipun";
}
//Retrieving value from application state
lock (this)
{
string str = Application["NickName"].ToString();
}

Code Sample
Server Side – Session State
 Place to store values that will persist across page requests
 Values stored in Session are stored on the server
 Values remain in memory until they are explicitly removed or until the Session expires
Server Side – Session State
Sample:
//Storing informaton in session state
Session["NickName"] = "ABC";
//Retrieving information from session state
string str = Session["NickName"];
Code Example:

object sessionObject = Session["someObject"];
if (sessionObject != null)
{
myLabel.Text = sessionObject.ToString();
}

Code Sample
Server Side – Session State

Code Sample

In Process mode:
 Default mode
 Session values are stored in the web server's memory (in IIS & for each IIS Server)
 Values will be lost on server restart

<configuration>
<sessionstate mode="InProc" />
</configuration>
Server Side – Session State

Code Sample

In State Server mode:
 Store data out of AppPool
 Needs to serialize objects
 Values will not be lost on server restart
See more details

<configuration>
<sessionstate mode="stateserver"
stateConnectionString="tcpip=127.0.0.1:42424" />
</configuration>
Server Side – Session State

Code Sample

In SQL Server mode:
 Highly secure and reliable

 Overhead from serialization and deserialization
 Should be used when reliability is more important than performance
 Setup mode in sql server by running InstallSqlState.sql
Possible Path for .Net 4.0 [C:WindowsMicrosoft.NETFramework64v4.0.30319]
<configuration>
<sessionstate mode="sqlserver"
sqlConnectionString="Data Source=(local);User ID=sa;Password=pwd" />
</configuration>
Server Side – Profile Properties
 Allows you to store user-specific data
 Unlike session state, the profile data is not lost when a user's session expires
 Uses an ASP.NET profile, which is stored in a persistent format and associated with an
individual user
 Each user has its own profile
Server Side – Profile
Sample:
<profile>
<properties>
<add item="item name" />
</properties>
</profile>
Code Example:
<authentication mode="Windows" />
<profile>
<properties>
<add name="FirstName"/>
<add name="LastName"/>
<add name="Age"/>
<add name="City"/>
</properties>
</profile>

Code Sample
Server Side – Application State

Code Sample

State management

Recommended usage

Application state

Use when you are storing infrequently changed, global information that is used by many users, and security is not an
issue. Do not store large quantities of information in application state.

Session state

Use when you are storing short-lived information that is specific to an individual session and security is an issue. Do
not store large quantities of information in session state. Be aware that a session-state object will be created and
maintained for the lifetime of every session in your application. In applications hosting many users, this can occupy
significant server resources and affect scalability.

Profile properties

Use when you are storing user-specific information that needs to be persisted after the user session is expired and
needs to be retrieved again on subsequent visits to your application.

Database support

Use when you are storing large amounts of information, managing transactions, or the information must survive
application and session restarts. Data mining is a concern, and security is an issue.
Links
 http://msdn.microsoft.com/en-us/library/75x4ha6s.ASPX
 http://www.c-sharpcorner.com/uploadfile/nipuntomar/state-management-in-Asp-Net/
 http://www.c-sharpcorner.com/uploadfile/37db1d/hour-1-understanding-5-Asp-Net-statemanagement-techniques-in-5-hours/

with examples:
 http://www.codeproject.com/Articles/492397/State-Management-in-ASP-NET-Introduction
 http://msdn.microsoft.com/en-us/library/ff647327.aspx

Mais conteúdo relacionado

Mais procurados

State management
State managementState management
State managementLalit Kale
 
State management
State managementState management
State managementteach4uin
 
ASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET WorksASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET WorksRandy Connolly
 
State management
State managementState management
State managementIblesoft
 
Programming web application
Programming web applicationProgramming web application
Programming web applicationaspnet123
 
Session 33 - Session Management using other Techniques
Session 33 - Session Management using other TechniquesSession 33 - Session Management using other Techniques
Session 33 - Session Management using other TechniquesPawanMM
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajaxNir Elbaz
 
Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010Rob Windsor
 
Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6PawanMM
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?DicodingEvent
 

Mais procurados (20)

AJAX
AJAXAJAX
AJAX
 
State management
State managementState management
State management
 
ASP.NET lecture 8
ASP.NET lecture 8ASP.NET lecture 8
ASP.NET lecture 8
 
State management
State managementState management
State management
 
Data Binding
Data BindingData Binding
Data Binding
 
ASP.NET Lecture 2
ASP.NET Lecture 2ASP.NET Lecture 2
ASP.NET Lecture 2
 
Understanding AJAX
Understanding AJAXUnderstanding AJAX
Understanding AJAX
 
Ajax
AjaxAjax
Ajax
 
Xml http request
Xml http requestXml http request
Xml http request
 
ASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET WorksASP.NET 02 - How ASP.NET Works
ASP.NET 02 - How ASP.NET Works
 
State management
State managementState management
State management
 
Programming web application
Programming web applicationProgramming web application
Programming web application
 
AJAX
AJAXAJAX
AJAX
 
Session 33 - Session Management using other Techniques
Session 33 - Session Management using other TechniquesSession 33 - Session Management using other Techniques
Session 33 - Session Management using other Techniques
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Asp.net
Asp.netAsp.net
Asp.net
 
Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010Data Access Options in SharePoint 2010
Data Access Options in SharePoint 2010
 
Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6
 
MVC.net training in pune
MVC.net training in pune MVC.net training in pune
MVC.net training in pune
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
 

Semelhante a State management

PCI Security Requirements - secure coding
PCI Security Requirements - secure codingPCI Security Requirements - secure coding
PCI Security Requirements - secure codingHaitham Raik
 
ASP.Net Presentation Part3
ASP.Net Presentation Part3ASP.Net Presentation Part3
ASP.Net Presentation Part3Neeraj Mathur
 
Bigger Stronger Faster
Bigger Stronger FasterBigger Stronger Faster
Bigger Stronger FasterChris Love
 
Developing your first application using FIWARE
Developing your first application using FIWAREDeveloping your first application using FIWARE
Developing your first application using FIWAREFIWARE
 
SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)Kashif Imran
 
State management
State managementState management
State managementIblesoft
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actionsAren Zomorodian
 
13 networking, mobile services, and authentication
13   networking, mobile services, and authentication13   networking, mobile services, and authentication
13 networking, mobile services, and authenticationWindowsPhoneRocks
 
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...SharePoint Saturday NY
 
Session viii(state mngtserver)
Session viii(state mngtserver)Session viii(state mngtserver)
Session viii(state mngtserver)Shrijan Tiwari
 
ASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin LauASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin LauSpiffy
 
RIA services exposing & consuming queries
RIA services exposing & consuming queriesRIA services exposing & consuming queries
RIA services exposing & consuming queriesiedotnetug
 
05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07Vivek chan
 
The complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide showThe complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide showSubhas Malik
 
TechDays 2013 Jari Kallonen: What's New WebForms 4.5
TechDays 2013 Jari Kallonen: What's New WebForms 4.5TechDays 2013 Jari Kallonen: What's New WebForms 4.5
TechDays 2013 Jari Kallonen: What's New WebForms 4.5Tieturi Oy
 
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...Dan Wahlin
 
State management in ASP.NET
State management in ASP.NETState management in ASP.NET
State management in ASP.NETOm Vikram Thapa
 

Semelhante a State management (20)

PCI Security Requirements - secure coding
PCI Security Requirements - secure codingPCI Security Requirements - secure coding
PCI Security Requirements - secure coding
 
ASP.Net Presentation Part3
ASP.Net Presentation Part3ASP.Net Presentation Part3
ASP.Net Presentation Part3
 
2310 b 14
2310 b 142310 b 14
2310 b 14
 
Bigger Stronger Faster
Bigger Stronger FasterBigger Stronger Faster
Bigger Stronger Faster
 
Developing your first application using FIWARE
Developing your first application using FIWAREDeveloping your first application using FIWARE
Developing your first application using FIWARE
 
SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)
 
State management
State managementState management
State management
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
 
Bh Win 03 Rileybollefer
Bh Win 03 RileybolleferBh Win 03 Rileybollefer
Bh Win 03 Rileybollefer
 
13 networking, mobile services, and authentication
13   networking, mobile services, and authentication13   networking, mobile services, and authentication
13 networking, mobile services, and authentication
 
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
Lyudmila Zharova: Developing Solutions for SharePoint 2010 Using the Client O...
 
Session viii(state mngtserver)
Session viii(state mngtserver)Session viii(state mngtserver)
Session viii(state mngtserver)
 
ASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin LauASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin Lau
 
RIA services exposing & consuming queries
RIA services exposing & consuming queriesRIA services exposing & consuming queries
RIA services exposing & consuming queries
 
05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07
 
The complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide showThe complete ASP.NET (IIS) Tutorial with code example in power point slide show
The complete ASP.NET (IIS) Tutorial with code example in power point slide show
 
TechDays 2013 Jari Kallonen: What's New WebForms 4.5
TechDays 2013 Jari Kallonen: What's New WebForms 4.5TechDays 2013 Jari Kallonen: What's New WebForms 4.5
TechDays 2013 Jari Kallonen: What's New WebForms 4.5
 
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
 
70562 (1)
70562 (1)70562 (1)
70562 (1)
 
State management in ASP.NET
State management in ASP.NETState management in ASP.NET
State management in ASP.NET
 

Último

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 

Último (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 

State management

  • 1. State Management MUHAMMAD AMIR – A M I R 4IT@ GMAI L.COM
  • 2. Why we need? Stateless HTTP Protocol:  No information about the request  Request created/destroyed with each post backversion.
  • 5. Client Side – View State  Mechanism that allows state values to be preserved across page postbacks  EnableViewState property will be set to true Advantages  No server resources are required  Simple implementation  Enhanced security features Disadvantages  Performance considerations  Device limitations  Potential security risks
  • 6. Client Side – View State  Sample: //To Save Information in View State ViewState.Add ("NickName", "Dolly"); //Retrieving View state String strNickName = ViewState ["NickName"];  Code Example: public int SomeInteger { get { object o = ViewState["SomeInteger"]; if (o != null) return (int)o; return 0; //a default } set { ViewState["SomeInteger"] = value; } } Code Sample
  • 7. Client Side – View State  Controls that use the view state
  • 8. Client Side – Control State  Essential for control to function properly  It is Private viewstate for the control only  Work even when ViewState turned off
  • 9. Client Side – Control State Code Sample public class ControlStateWebControl : Control { private string _strStateToSave; protected override void OnInit(EventArgs e) { Page.RegisterRequiresControlState(this); base.OnInit(e); } protected override object SaveControlState() { return _strStateToSave; } protected override void LoadControlState(object state) { if (state != null) { _strStateToSave = state.ToString(); } } }
  • 10. Client Side – Hidden Fields  Not rendered in browser  Use to store small frequently changed data
  • 11. Client Side – Hidden Fields Code Sample protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack)| Label1.Text = string.Format("Clicked {0} times", HiddenField1.Value); } protected void Button1_Click(object sender, EventArgs e) { HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value) + 1).ToString(); } Label1.Text = string.Format("Clicked {0} times", HiddenField1.Value);
  • 12. Client Side – Cookies  Stored at client side in text file or in memory of the client browser session  Always sent with the request to the web server  Read from user machine to identify user
  • 13. Client Side – Cookies Code Sample //Storing value in cookie HttpCookie cookie = new HttpCookie("NickName"); cookie.Value = "David"; Request.Cookies.Add(cookie); //Retrieving value in cookie if (Request.Cookies.Count > 0 && Request.Cookies["NickName"] != null) lblNickName.Text = "Welcome" + Request.Cookies["NickName"].ToString(); else lblNickName.Text = "Welcome Guest";
  • 14. Client Side – Query String  Used to pass information across pages  Information passed along with URL in clear text
  • 15. Client Side – Query String public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { string v = Request.QueryString["param"]; if (v != null) { Response.Write("param is "); Response.Write(v); } string x = Request.QueryString["id"]; if (x != null) { Response.Write(" id detected"); } } } Code Sample
  • 16. Client Side – Summary
  • 17. Client Side – Summary State management Recommended usage View state Use when you need to store small amounts of information for a page that will post back to itself. Using the ViewState property provides functionality with basic security. Control state Hidden fields Cookies Use when you need to store small amounts of state information for a control between round trips to the server. Use when you need to store small amounts of information for a page that will post back to itself or to another page, and when security is not an issue. Note You can use a hidden field only on pages that are submitted to the server. Use when you need to store small amounts of information on the client and security is not an issue. Use when you are transferring small amounts of information from one page to another and security is not an issue. Query string Note You can use query strings only if you are requesting the same page, or another page via a link.
  • 20. Server Side – Application State  Used to share information among users  Stored in the memory of the windows process  Great place to store data that changes infrequently
  • 21. Server Side – Application State protected void Page_Load(object sender, EventArgs e) { int count = 0; if (Application["HitCounterForOrderPage"] != null) count = (int)Application["HitCounterForOrderPage"]; count++; Application["HitCounterForOrderPage"] = count; lblcounter.Text = "Page Visited: " + count.ToString() + "Times"; } //Application.Lock(); and Application.UnLock(); Code Sample
  • 22. Server Side – Application State //Stroing information in application state lock (this) { Application["NickName"] = "Nipun"; } //Retrieving value from application state lock (this) { string str = Application["NickName"].ToString(); } Code Sample
  • 23. Server Side – Session State  Place to store values that will persist across page requests  Values stored in Session are stored on the server  Values remain in memory until they are explicitly removed or until the Session expires
  • 24. Server Side – Session State Sample: //Storing informaton in session state Session["NickName"] = "ABC"; //Retrieving information from session state string str = Session["NickName"]; Code Example: object sessionObject = Session["someObject"]; if (sessionObject != null) { myLabel.Text = sessionObject.ToString(); } Code Sample
  • 25. Server Side – Session State Code Sample In Process mode:  Default mode  Session values are stored in the web server's memory (in IIS & for each IIS Server)  Values will be lost on server restart <configuration> <sessionstate mode="InProc" /> </configuration>
  • 26. Server Side – Session State Code Sample In State Server mode:  Store data out of AppPool  Needs to serialize objects  Values will not be lost on server restart See more details <configuration> <sessionstate mode="stateserver" stateConnectionString="tcpip=127.0.0.1:42424" /> </configuration>
  • 27. Server Side – Session State Code Sample In SQL Server mode:  Highly secure and reliable  Overhead from serialization and deserialization  Should be used when reliability is more important than performance  Setup mode in sql server by running InstallSqlState.sql Possible Path for .Net 4.0 [C:WindowsMicrosoft.NETFramework64v4.0.30319] <configuration> <sessionstate mode="sqlserver" sqlConnectionString="Data Source=(local);User ID=sa;Password=pwd" /> </configuration>
  • 28. Server Side – Profile Properties  Allows you to store user-specific data  Unlike session state, the profile data is not lost when a user's session expires  Uses an ASP.NET profile, which is stored in a persistent format and associated with an individual user  Each user has its own profile
  • 29. Server Side – Profile Sample: <profile> <properties> <add item="item name" /> </properties> </profile> Code Example: <authentication mode="Windows" /> <profile> <properties> <add name="FirstName"/> <add name="LastName"/> <add name="Age"/> <add name="City"/> </properties> </profile> Code Sample
  • 30. Server Side – Application State Code Sample State management Recommended usage Application state Use when you are storing infrequently changed, global information that is used by many users, and security is not an issue. Do not store large quantities of information in application state. Session state Use when you are storing short-lived information that is specific to an individual session and security is an issue. Do not store large quantities of information in session state. Be aware that a session-state object will be created and maintained for the lifetime of every session in your application. In applications hosting many users, this can occupy significant server resources and affect scalability. Profile properties Use when you are storing user-specific information that needs to be persisted after the user session is expired and needs to be retrieved again on subsequent visits to your application. Database support Use when you are storing large amounts of information, managing transactions, or the information must survive application and session restarts. Data mining is a concern, and security is an issue.
  • 31. Links  http://msdn.microsoft.com/en-us/library/75x4ha6s.ASPX  http://www.c-sharpcorner.com/uploadfile/nipuntomar/state-management-in-Asp-Net/  http://www.c-sharpcorner.com/uploadfile/37db1d/hour-1-understanding-5-Asp-Net-statemanagement-techniques-in-5-hours/ with examples:  http://www.codeproject.com/Articles/492397/State-Management-in-ASP-NET-Introduction  http://msdn.microsoft.com/en-us/library/ff647327.aspx