SlideShare una empresa de 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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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?
 

Similar 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
 

Similar 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

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
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
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
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
 
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
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 

Último (20)

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
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
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
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
 
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
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 

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