Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Session Checking In Class File

Session Checking In Class File

Scheduled Pinned Locked Moved ASP.NET
questionhtmlcomdesignhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #1

    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 inherit System.Web.UI.Page, Instead of this I inherited this in my CAuthentication class. Where I am going wrong ?


    My Website | Ask smart questions

    N _ 2 Replies Last reply
    0
    • N N a v a n e e t h

      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 inherit System.Web.UI.Page, Instead of this I inherited this in my CAuthentication class. Where I am going wrong ?


      My Website | Ask smart questions

      N Offline
      N Offline
      Nicejith
      wrote on last edited by
      #2

      Please check it with HttpContext.Current.Session["login"].

      N 1 Reply Last reply
      0
      • N N a v a n e e t h

        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 inherit System.Web.UI.Page, Instead of this I inherited this in my CAuthentication class. Where I am going wrong ?


        My Website | Ask smart questions

        _ Offline
        _ Offline
        _mubashir
        wrote on last edited by
        #3

        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.

        N 1 Reply Last reply
        0
        • N Nicejith

          Please check it with HttpContext.Current.Session["login"].

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Nicejith wrote:

          HttpContext.Current.Session["login"].

          Thanks, But still it shows Object Reference error.,


          My Website | Ask smart questions

          1 Reply Last reply
          0
          • _ _mubashir

            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.

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            _mubashir wrote:

            Why do you need to do this??

            Thanks, I need to do this because, this simplifies my job than passing Response, and SessionState 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 ?


            My Website | Ask smart questions

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups