how to expire session in asp.net

Posted by Shanky11 under ASP.NET on 3/1/2013 | Points: 10 | Views : 5179 | Status : [Member] | Replies : 8
i have a login page and home page
after successfull login aand visting the site user click on logout button
then if we ty to copy the url og home page it should goes to login page
please help me for the same




Responses

Posted by: Santhi on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

You need to abandon the session values
on the click event write the following code.
Session.Abandon();

Thanks & Regards,
Santhi .V

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
hello Santhi
Error is comng see i m pasting my code below
Login page-----------------------------
protected void btnlogin_Click(object sender, EventArgs e)
{
if((txtusername.Text != " ")&&(txtpassword.Text != " "))
{
Session["username"] = txtusername.Text;
Response.Redirect("HomePage.aspx");
}
---------------------------------------HOME PAGE--------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"].ToString() == null)
{
Response.Redirect("LoginPage.aspx");
}
else
lblwelcome.Text = " Welcome " + Session["username"].ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
Session.Abandon();
}

}

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Murugavelmsc on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

replace the Session.Abandon() as Session.Clear()

Plz confirm if it wokrs...




Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
i had tried it but same error
object ref not set to instanc e of obj

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

you can write that code in global.asax file.

Thanks & Regards,
Santhi .V

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Murugavelmsc on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

whether your query has been resolved or not?



Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 3/1/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello Shanky11,

Check this.

I have taken 2 Pages

1.Login.aspx
2.Welcome.aspx

If the user inputs the username and password i am storing the username in the session and redirecting them to Welcome.aspx
and if Session is null then it will redirect to login page.

On the Welcome page there is a Logout Button.On clicking it the session will expire and the user will be redirected to login page.

When the user open the tab and tries to call directly the welcome.aspx he will be redirected to login check this code.

Login.aspx

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

<!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>
<asp:TextBox ID="xtxtUserName" runat="server"></asp:TextBox><br />
<asp:TextBox ID="xtxtPassword" runat="server"></asp:TextBox><br />
<asp:Button ID="xLogin" runat="server" Text="Button" onclick="Login_Click" />

</div>

</form>
</body>
</html>


Code behind.

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

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

}
protected void Login_Click(object sender, EventArgs e)
{
string Username = xtxtUserName.Text;
string password = xtxtPassword.Text;
Session["Username"] = xtxtUserName.Text;
Response.Redirect("~/Welcome.aspx");
}
}

Welcome.aspx

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

<!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>

<asp:Label ID="lblUsername" runat="server" Text=""></asp:Label>
&nbsp;&nbsp; Welcome&nbsp;
<asp:LinkButton ID="xlnklogout" runat="server" onclick="xlnklogout_Click">Log Out</asp:LinkButton>
</div>
</form>
</body>
</html>

Code behind of welcome page

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

public partial class Welcome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Username"] == null)
{
Response.Redirect("~/Login.aspx");
}
else
{
lblUsername.Text = Session["Username"].ToString();
}
}
}
protected void xlnklogout_Click(object sender, EventArgs e)
{
Session.Clear();
Session.Abandon();
Response.Redirect("~/Login.aspx");
}
}



Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 3/2/2013 [Member] Bronze | Points: 25

Up
0
Down
Use the Function "Session.Abandon()" to Kill the User Sessions


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response