Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 22 October 2013

Session in Asp.Net

Http Protocol is stateless protocol.

View state is accessible only with in same page[we have already seen]
Session variables can be accessed across pages.

Session is private to the users.  Actual data get stored on server.
Next things is how does server identifies whose data is whose.
If data is stored in the server and the server uses http protocol ...then server doesnt knows whose data is whose.

Asp.net has taken care of it. For every user it has create  a key.
Asp.net stores all the information of session variables in the server but then it sends the key to the browser or the end user who is surfing the site. And the key is stored as the cookies file. Cookie is nothing but simple text file which is created in the browser folder.
So session data is stored on the server and keys are stored in cookies files.
If cookie is disabled then sesssion id key is passed via query string.


1)The session state is used to maintain state value in the server machine.
   The session state maintains the values in the session object memory.

2) 20 Min by default.

you can extend it to 1000 min...





























-----------------------------------------------default.aspx---------------------------------------------------------


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    UserName&nbsp;
    <asp:TextBox ID="txtname" runat="server" style="margin-right: 0px"></asp:TextBox>
    <br />
    Password&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtpassword" runat="server"  
        style="margin-left: 0px; margin-bottom: 0px"></asp:TextBox>
    &nbsp;
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Sign In" 
        onclick="Button1_Click" />
    </form>
</body>
</html>




---------------------------------------------default.cs---------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {

        if (txtname.Text == "anurag" && txtpassword.Text == "123")
        {

                Session["abc"] = txtname.Text;
            Session.Timeout = 1;
            Response.Redirect("~/Default2.aspx");
        }
        else 
        {

            Response.Write("wrong username or password");
            txtname.Text ="";
            txtpassword.Text ="";

        }
        

    }
}

------------default2.aspx design-------------------------------------------------------------------------









-------------------------------------------default2.aspx------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    
        Hello Welcome To Our Site
        <asp:Label ID="Label1" runat="server"></asp:Label>
    
    
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        Text="signout" />
    </form>
</body>
</html>



--------------------------------------------default2.cs-------------------------------------------------


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["abc"] == null)
        {

            Response.Redirect("~/Default.aspx");

        }
        else
        {
            Label1.Text = Session["abc"].ToString();

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        Session.Remove("abc");
    //if you comment the above line then you can't log out

        if (Session["abc"] == null)
        {

            Response.Redirect("~/Default.aspx");

        }
    }
}

-------------------------------------------out put-------------------------------------------------------



















No comments:

Post a Comment