Next


Limitations of “Cookies”

• Cookies will be stored in the form of files in the “Temporary Internet Files” folder.
• They can be easily visible by other users. It is easy to modify or delete the cookie files. So, no security is provided.
• Cookie files will not be deleted automatically. So that, it leads to waste of hard disk memory.
• It doesn’t supports to share objects among multiple pages.

Session State
• Whenever a client (browser) is connected, the “Session” state will be created automatically; and the session will be closed automatically whenever the client (browser) is disconnected.
• For every client (browser), a separate session will be created.
• Similar to Cookies, the “Session state” is used to share values among multiple pages. For example, between login and logout, the current user name, user id number and password will be stored in the session state.
• Now-a-days, “Cookies” are outdated. Instead of cookies, sessions are used.
• The main difference between cookies and session state is: a cookie will be saved on the client system and the session will be saved at web sever.
• Sessions are more secured, when compared with “Cookies”, as sessions will be saved at web server.
• Sessions will not be saved in the form of any files.
• Sessions are able to share values and objects also.
• Implementation of “Session state” is easier, when compared with “Cookies”.

Implementation:

A. Assign value to the Session:
Session[“name”] = value;
B. Get value from the Session (gets the session variable value in “Object” type):
Session[“name”]
session

Default.aspx.cs
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = TextBox1.Text;
        Session["username"] = name;
        Response.Redirect("display.aspx");
    }
display.aspx.cs
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Convert.ToString(Session["username"]);
        Label1.Text = "Current User: " + name;
    }


Application State

• Another state available in ASP.NET is “Application” state. Similar to “Session state”, the “Application state” holds the data values or objects among multiple web pages in the web site.
• Similar to “Session state”, the “Application state” also will be stored at web server. It offers much security.
• The only difference between “Session” and “Application” states is: “The Session state data will be lost automatically whenever the browser is closed. But the Application state data will not be lost even after the browser is closed. It will remain until you stop the web server”.
• In other words, the session is limited to single client. Individual sessions are created for every client. But for all the clients, “Application” state is accessible.
• Conclusion: When you want to store any data value, that is related one user, you prefer to use “Session” state. Whenever your data value is to be accessible from other clients also, you prefer “Application” state.
Implementation: A. Assign value to the Application:
Application[“name”] = value;
B. Get value from the Application (gets the application state variable value in “Object” type):
Application[“name”]

Demo on Application State

application

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Application["ClicksCount"] != null)
        {
            int c = Convert.ToInt32(Application["ClicksCount"]);
            Label1.Text = "The button is clicked " + c + " time(s).";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int c = 0;
        if (Application["ClicksCount"] != null)
            c = Convert.ToInt32(Application["ClicksCount"]);
        c++;
        Application["ClicksCount"] = c;
        Label1.Text = "The button is clicked " + c + " time(s).";
    }