Session Example in ASP.Net C#
In previous post we learnt about session state management in asp.net. we also learnt how to assign value to session and retrieve value from session in asp.net. This asp.net tutorial we will understand more about session by tacking an example using c#.
Example
- Sign out automatically from website after some time
- Got message like “Your Session is Expired”
- After login to site user can view some web pages
Sign out automatically from website after some time
After login you stay idle on some period of time on any website, then after you do any activity on site and you automatically have been sign out from website and they ask to us login again.
In above case we need to use session timeout property. At the time login to site, the identification of user may be username or userid stored in session. We assign timeout value in minute to session timeout property. The timeout value is a length of time to live a session if we do nothing on site more than timeout value, then the session automatically time outed and session would be null. On each page while page loading time we need to check session value if there is some value found so go smoothly, but if session time out then redirect user to login page.
Example to understand session Timeout
Open visual studio add web forms in website, design static login page which allow to login user with valid username and password. here we use “meera” as username and “123” for password for login.
We have three web forms “Login.aspx” for login, “Main.aspx” display account information and “gallary.aspx” for display images.
On login page we do coding for login into website, if we login success then we are at on main.aspx page. after login we can check both myaccount and gallery page but if we do nothing up to 2 minute and then after click anywhere on page, that time automatically signed out , means redirect to login page.
Session Timeout
First Set session Timeout value in web.config file. Session timeout value by default set 20 minutes, we can change as per our requirement.
<system.web>
<sessionState timeout=”2″></sessionState>
</system.web>
Login.aspx Page
Design login page as show below screen and write below c# code on login button click events.
First check username and password values, if it is right then assign username value to session[“uname”] and send user to Main.aspx page using response.redirect. If credential are wrong then display error message.
protected void btnlogin_Click(object sender, EventArgs e)
{
if (txtuname.Text == "meera" && txtupass.Text == "123")
{
Session["uname"] = txtuname.Text;
Response.Redirect("Main.aspx");
}
else
{
lbl.Text = "Invalid detail";
}
}
Main.aspx and Gallary.aspx Page
In both page in page load events write below c# code. While page loading every time check session[“uname”] value if it is null then send user to login page other wise go aheacled to site and display username on page.
Here, if we idle for more than 2 minutes on site then after click any where on site the session would be null and we redirected to login page.
On logout button we write code for redirect to login page after clearing the session[“uname”].
protected void Page_Load(object sender, EventArgs e)
{
if (Session["uname"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
lblname.Text = "Welcome " + Session["uname"].ToString();
}
}
protected void btnlogout_Click(object sender, EventArgs e)
{
Session["uname"] = null;
Response.Redirect("Login.aspx");
}
Got message like “Your Session is Expired”
In above example both main.aspx and gallary.aspx form in page_load events check if session[“uname”]=null then write message in label like “Your Session is Expired” instead of redirected to login page.
Related ASP.Net Topics :
Session State in C#.Net
View State in C#.Net
Subscribe us
If you liked this asp.net post, then please subscribe to our YouTube Channel for more asp.net video tutorials.
We hope that this asp.net c# tutorial helped you to understand more about Session with example.
Next asp.net tutorial we will understand difference between Session Clear() and Session Abandon().
Very helpful.