Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jun 17, 2010 12:37 PM by balamurugavel
Member
139 Points
65 Posts
Mar 02, 2007 08:01 PM|LINK
How to check whether a session exists or not??
I want to do this on each page, if not exist I will just send the user back to the sign in page....
Contributor
4222 Points
797 Posts
Mar 02, 2007 08:21 PM|LINK
The session will always exist probably because when your session expires and you access a page again it just makes up a new session for you.
One thing you can do is the first time you access the app just set a session variable like:
Session["LoggedIn"] = "1";
Then check to see if it exists. If it doesn't then you know the session has been expired and recreated so you can boot them back.
if (Session["LoggedIn"] == null) { Response.Redirect("YourSignInPage.aspx"); } else { // Do whatever you were going to do. }
320 Points
51 Posts
Jun 16, 2010 10:44 AM|LINK
Prefer events available in global.asax.
You can have session validation in each and every form load.
But prefering event Session_Start with a help of another Session can help the application lot in architecture and also in performance.
The sample code ...
void Session_Start(object sender, EventArgs e) { if (Session.IsNewSession && Session["SessionExpire"]==null) { Session["SessionExpire"] = true; Response.Redirect("Welcome.aspx"); } }
Happy Coding...
bala
global.asax session expire session validation
Star
11118 Points
1997 Posts
Jun 16, 2010 11:07 AM|LINK
create a common function to check session value.
Public Class common
Public Sub CheckLogin(ByVal pg As Web.UI.Page) If IsNothing(pg.Session.Item("UserName")) Then pg.Response.Redirect("login.aspx") End If End Sub
End Class
and call to each page laod
Dim clsCommon As New common clsCommon.CheckLogin(Me.Page)
C#.net
public void fnCheckLogin(Page pg) { if (string.IsNullOrEmpty((string)pg.Session["username"])) { pg.Response.Redirect("login.aspx"); } }
how to call
clsCommon.fnCheckLogin(this.Page);
happy coding
Participant
776 Points
183 Posts
Jun 16, 2010 11:21 AM|LINK
Nice Programmer How to check whether a session exists or not?? I want to do this on each page, if not exist I will just send the user back to the sign in page....
Use this code for Session_Start event in global.asax file:
void Session_Start(object sender, EventArgs e) { if (User.Identity.IsAuthenticated) { System.Web.Security.FormsAuthentication.SignOut(); System.Web.Security.FormsAuthentication.RedirectToLoginPage(); } }
4270 Points
735 Posts
Jun 16, 2010 01:19 PM|LINK
You dont need to copy paste your code in all pages.
Simple make a user control and on Page_Load check if session exists or not if not then rediect to main page
copy this in master page or user control
if(Session["isUser"] ==null)
{
Response.Redirect("~/Login.aspx");
}
Jun 17, 2010 12:37 PM|LINK
if(Session["SessionName"] == null) { //Session Does not Exists } else { //Session Exists }
session exists or not
Nice Program...
Member
139 Points
65 Posts
How to check whether a session exists or not??
Mar 02, 2007 08:01 PM|LINK
How to check whether a session exists or not??
I want to do this on each page, if not exist I will just send the user back to the sign in page....
ASP.NET(1.1/2.0) == PHP
Kelsey
Contributor
4222 Points
797 Posts
Re: How to check whether a session exists or not??
Mar 02, 2007 08:21 PM|LINK
The session will always exist probably because when your session expires and you access a page again it just makes up a new session for you.
One thing you can do is the first time you access the app just set a session variable like:
Session["LoggedIn"] = "1";
Then check to see if it exists. If it doesn't then you know the session has been expired and recreated so you can boot them back.
if (Session["LoggedIn"] == null) { Response.Redirect("YourSignInPage.aspx"); } else { // Do whatever you were going to do. }balamurugave...
Member
320 Points
51 Posts
Re: How to check whether a session exists or not??
Jun 16, 2010 10:44 AM|LINK
Prefer events available in global.asax.
You can have session validation in each and every form load.
But prefering event Session_Start with a help of another Session can help the application lot in architecture and also in performance.
The sample code ...
void Session_Start(object sender, EventArgs e) { if (Session.IsNewSession && Session["SessionExpire"]==null) { Session["SessionExpire"] = true; Response.Redirect("Welcome.aspx"); } }Happy Coding...
bala
global.asax session expire session validation
Bala
nareshguree2...
Star
11118 Points
1997 Posts
Re: How to check whether a session exists or not??
Jun 16, 2010 11:07 AM|LINK
create a common function to check session value.
Public Class common
Public Sub CheckLogin(ByVal pg As Web.UI.Page)
If IsNothing(pg.Session.Item("UserName")) Then
pg.Response.Redirect("login.aspx")
End If
End Sub
End Class
and call to each page laod
Dim clsCommon As New common
clsCommon.CheckLogin(Me.Page)
C#.net
public void fnCheckLogin(Page pg)
{
if (string.IsNullOrEmpty((string)pg.Session["username"]))
{
pg.Response.Redirect("login.aspx");
}
}
how to call
clsCommon.fnCheckLogin(this.Page);
happy coding
vikasrulez
Participant
776 Points
183 Posts
Re: How to check whether a session exists or not??
Jun 16, 2010 11:21 AM|LINK
Use this code for Session_Start event in global.asax file:
void Session_Start(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
System.Web.Security.FormsAuthentication.SignOut();
System.Web.Security.FormsAuthentication.RedirectToLoginPage();
}
}
Aamir Hasan
Contributor
4270 Points
735 Posts
Re: How to check whether a session exists or not??
Jun 16, 2010 01:19 PM|LINK
You dont need to copy paste your code in all pages.
Simple make a user control and on Page_Load check if session exists or not if not then rediect to main page
copy this in master page or user control
if(Session["isUser"] ==null)
{
Response.Redirect("~/Login.aspx");
}
Please Mark as Answered If its helpful you.
balamurugave...
Member
320 Points
51 Posts
Re: How to check whether a session exists or not??
Jun 17, 2010 12:37 PM|LINK
if(Session["SessionName"] == null) { //Session Does not Exists } else { //Session Exists }Have this code in the Page_Load()
session exists or not
Bala