Session Checking In Class File
-
I created a class which checks session and redirects to login page. My class looks like this. This class inherits
Page
class to get access to session object.public class CAuthentication:System.Web.UI.page
{
public CAuthentication()
{
if ( Session["login"] == null )
Response.Redirect("expired.aspx");
}
}My ASPX.CS looks like
public partial class MyPage:CAuthentication
{
//Pageload goes here
}This is not working. I am getting error
"session can't be used inside a class"
. What is wrong here ? Normally codebehind file used to inheritSystem.Web.UI.Page
, Instead of this I inherited this in myCAuthentication
class. Where I am going wrong ?
-
I created a class which checks session and redirects to login page. My class looks like this. This class inherits
Page
class to get access to session object.public class CAuthentication:System.Web.UI.page
{
public CAuthentication()
{
if ( Session["login"] == null )
Response.Redirect("expired.aspx");
}
}My ASPX.CS looks like
public partial class MyPage:CAuthentication
{
//Pageload goes here
}This is not working. I am getting error
"session can't be used inside a class"
. What is wrong here ? Normally codebehind file used to inheritSystem.Web.UI.Page
, Instead of this I inherited this in myCAuthentication
class. Where I am going wrong ?
-
I created a class which checks session and redirects to login page. My class looks like this. This class inherits
Page
class to get access to session object.public class CAuthentication:System.Web.UI.page
{
public CAuthentication()
{
if ( Session["login"] == null )
Response.Redirect("expired.aspx");
}
}My ASPX.CS looks like
public partial class MyPage:CAuthentication
{
//Pageload goes here
}This is not working. I am getting error
"session can't be used inside a class"
. What is wrong here ? Normally codebehind file used to inheritSystem.Web.UI.Page
, Instead of this I inherited this in myCAuthentication
class. Where I am going wrong ?
N a v a n e e t h wrote:
Normally codebehind file used to inherit System.Web.UI.Page, Instead of this I inherited this in my CAuthentication class.
Why do you need to do this?? For your session check, you can use your existing class and I suggest you should pass the Session object to your session checker function and then check the sessions and proceed to redirection. Like
public CheckAuthentication(System.Web.SessionState.HttpSessionState oSession ) { if ( oSession["login"] == null ) Response.Redirect("expired.aspx"); }
Also pass the response object the same way. NOTE: Remove the inheritance from your class
Mubashir Software Architect Storan Technologies Inc, USA Every job is a self portrait of the person who did it.
-
Nicejith wrote:
HttpContext.Current.Session["login"].
Thanks, But still it shows Object Reference error.,
-
N a v a n e e t h wrote:
Normally codebehind file used to inherit System.Web.UI.Page, Instead of this I inherited this in my CAuthentication class.
Why do you need to do this?? For your session check, you can use your existing class and I suggest you should pass the Session object to your session checker function and then check the sessions and proceed to redirection. Like
public CheckAuthentication(System.Web.SessionState.HttpSessionState oSession ) { if ( oSession["login"] == null ) Response.Redirect("expired.aspx"); }
Also pass the response object the same way. NOTE: Remove the inheritance from your class
Mubashir Software Architect Storan Technologies Inc, USA Every job is a self portrait of the person who did it.
_mubashir wrote:
Why do you need to do this??
Thanks, I need to do this because, this simplifies my job than passing
Response
, andSessionState
object to Checking functions. In all my pages I need to check this session. So i thought it's better to go for inheritance method. May be that is a bad approach, but still it has to work when we think the program flow. Any Idea on what is going wrong here ?