List Info

Thread: Access session in abstract class




Access session in abstract class
country flaguser name
United States
2007-10-13 19:43:34
Hi,

Currently I am working on a web application which uses
ASP.NET 2.0.
Development language is C#

Inspired from the article http://odetoco
de.com/Articles/450.aspx , I
decided to encapsulate some of the basic functionalities
which my
pages need in a base class and inherit all the pages from
the base
class.

Code in my base class reads as:

public abstract class SecureBase:System.Web.UI.Page
{
    protected clsUM.USERINFO UINFO = new clsUM.USERINFO();
	public SecureBase()
	{
        UINFO =
(clsUM.USERINFO)Session["USER_INFO"];
        if (!clsUM.AuthorizeUser(101, UINFO.U_GROUP))
           
Response.Redirect("AccessWarning.aspx", false);
        }
}

In the page, I inherit it as

public partial class AdminMenu : SecureBase
{
   .....
   .....
}


However, It causes an runtime exception on line
        UINFO =
(clsUM.USERINFO)Session["USER_INFO"];

The exception message says:

{"Session state can only be used when
enableSessionState is set to
true, either in a configuration file or in the Page
directive. Please
also make sure that System.Web.SessionStateModule or a
custom session
state module is included in the
<configuration><system.web>
<httpModules> section in the application
configuration."}
System.Exception {System.Web.HttpException}

I tried to put the said directives in web.config but nothing
to help.

I think the question is of fundamentals. Anyone can please
tell me
whether
1. Is it possible to put some common features required by
all pages in
a class and inherit all pages from that class to avail all
those
features?
2. Can I use session variable in a class, If I can, what is
the
correct way to do so.
3. Does it make any difference if the class is abstract or
else


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "DotNetDevelopment, VB.NET, C# .NET,
ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting"
group.
To post to this group, send email to DotNetDevelopmentgooglegroups.com
To unsubscribe from this group, send email to
DotNetDevelopment-unsubscribegooglegroups.com
For more options, visit this group at
http:
//cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---


Re: Access session in abstract class
user name
2007-10-14 05:55:40
I don't know if you "can&quot; do this, but you "shouldn't&quot; -- this isn't good architecture -- it's poor encapsulation. If you want to access that value, pass it into a constructor, or explicitly set it through a property accessor. An object should never rely on an external dependency that isn't another object in scope, or passed in as a parameter, or set as a property. You should be able to declare that object anywhere, anytime, pass it the parameter, and get the expected behavior. Declaring an object that depends strictly on an ASP.NET environment isn't good OO.

Have you looked into using CurrentPrincipal and CurrentIdentity, and how to use them through the  Context and Thread objects provided for you?

--Andrew Badera


On 10/13/07, Murtuza < murtuzakabulgmail.com">murtuzakabulgmail.com> wrote:

Hi,

Currently I am working on a web application which uses ASP.NET 2.0.
Development language is C#

Inspired from the article http://odetocode.com/Articles/450.aspx , I
decided to encapsulate some of the basic functionalities which my
pages need in a base class and inherit all the pages from the base
class.

Code in my base class reads as:

public abstract class SecureBase:System.Web.UI.Page
{
  ; &nbsp;protected clsUM.USERINFO UINFO = new clsUM.USERINFO();
&nbsp; &nbsp; &nbsp;   ;public SecureBase()
 &nbsp; &nbsp;   ; {
&nbsp; &nbsp;   ; &nbsp;UINFO = (clsUM.USERINFO)Session["USER_INFO&quot;];
&nbsp; &nbsp; &nbsp; &nbsp; if (!clsUM.AuthorizeUser(101, UINFO.U_GROUP))
 ; &nbsp; &nbsp; &nbsp; &nbsp;   ;Response.Redirect(&quot;AccessWarning.aspx&quot;, false);
&nbsp; &nbsp;   ; &nbsp;}
}

In the page, I inherit it as

public partial class AdminMenu : SecureBase
{
   .....
&nbsp;  .....
}


However, It causes an runtime exception on line
 ; &nbsp; &nbsp; &nbsp; UINFO = (clsUM.USERINFO)Session["USER_INFO&quot;];

The exception message says:

{&quot;Session state can only be used when enableSessionState is set to
true, either in a configuration file or in the Page directive. Please
also make sure that System.Web.SessionStateModule or a custom session
state module is included in the <configuration><system.web>
&lt;httpModules> section in the application configuration."}
System.Exception {System.Web.HttpException}

I tried to put the said directives in web.config but nothing to help.

I think the question is of fundamentals. Anyone can please tell me
whether
1. Is it possible to put some common features required by all pages in
a class and inherit all pages from that class to avail all those
features?
2. Can I use session variable in a class, If I can, what is the
correct way to do so.
3. Does it make any difference if the class is abstract or else

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting&quot; group.
To post to this group, send email to DotNetDevelopmentgooglegroups.com
To unsubscribe from this group, send email to DotNetDevelopment-unsubscribegooglegroups.com
For more options, visit this group at
http://cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---

Re: Access session in abstract class
user name
2007-10-14 05:58:08
http://www.clixsense.com/?2078169
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting&quot; group.
To post to this group, send email to DotNetDevelopmentgooglegroups.com
To unsubscribe from this group, send email to DotNetDevelopment-unsubscribegooglegroups.com
For more options, visit this group at
http://cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---

Re: Access session in abstract class
country flaguser name
United States
2007-10-15 04:30:59
Hi,

  I think you have got a point. It is indeed a poor OO
practice and
should be avoided. However, I've got some situations to use
such kind
of programming.

  This is not a big application and designing n-tire
architecture
would be an overkill. Just a few pages which manages to
display lists
from the database. The only thing to concern is not all
users are
allowed to access all lists.

  My sole intention for such coding is to avoid replicating
same code
again and again in each page which checks the permission and
if the
user is not permitted, redirect it to another page. The
objective can
be easily achieved by simply putting the code in each page
but it
would be rather worse coding practice.

   I've also made up my mind to implement database
connection in the
base class so as all the derived class have a ready to use
database
connection.

  Meanwhile, I have done some research on it and I've come
to a
conclusion that session variables can't be accessed from a
class.
However some solutions suggest that they can be accessed
using
System.web.HttpContext.current.session but it is not
working. I have
no idea what OO principle is compromised or any other
technical reason
for such behavior.

  If you have any better solution for such problem, it would
be very
helpful.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "DotNetDevelopment, VB.NET, C# .NET,
ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting"
group.
To post to this group, send email to DotNetDevelopmentgooglegroups.com
To unsubscribe from this group, send email to
DotNetDevelopment-unsubscribegooglegroups.com
For more options, visit this group at
http:
//cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---


Re: Access session in abstract class
country flaguser name
United States
2007-10-14 03:05:21
I use a similar way to authenticate user identity.
But I don't make the class "abstract" .  I can't
tell the difference
between abstract or not  clearly.
So my code is :
public class SecureBase:Page {}
But it works fine.

I don't find any text about 'state'/'session' in my
web.config .
So I think this might not be the problem of configuration
file or page
directive.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "DotNetDevelopment, VB.NET, C# .NET,
ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting"
group.
To post to this group, send email to DotNetDevelopmentgooglegroups.com
To unsubscribe from this group, send email to
DotNetDevelopment-unsubscribegooglegroups.com
For more options, visit this group at
http:
//cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---


Re: Access session in abstract class
user name
2007-10-15 09:37:56
Using the current Thread and Context, you can achieve the sort of reduction in redundancy you appear to be aiming for. That property is already available "globally" as part of the Context of the Request.

--Andrew Badera



On 10/15/07, Murtuza < murtuzakabulgmail.com">murtuzakabulgmail.com> wrote:

Hi,

&nbsp; I think you have got a point. It is indeed a poor OO practice and
should be avoided. However, I've got some situations to use such kind
of programming.

 &nbsp;This is not a big application and designing n-tire architecture
would be an overkill. Just a few pages which manages to display lists
from the database. The only thing to concern is not all users are
allowed to access all lists.

&nbsp; My sole intention for such coding is to avoid replicating same code
again and again in each page which checks the permission and if the
user is not permitted, redirect it to another page. The objective can
be easily achieved by simply putting the code in each page but it
would be rather worse coding practice.

 &nbsp; I've also made up my mind to implement database connection in the
base class so as all the derived class have a ready to use database
connection.

 &nbsp;Meanwhile, I have done some research on it and I've come to a
conclusion that session variables can't be accessed from a class.
However some solutions suggest that they can be accessed using
System.web.HttpContext.current.session but it is not working. I have
no idea what OO principle is compromised or any other technical reason
for such behavior.

 &nbsp;If you have any better solution for such problem, it would be very
helpful.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting&quot; group.
To post to this group, send email to DotNetDevelopmentgooglegroups.com
To unsubscribe from this group, send email to DotNetDevelopment-unsubscribegooglegroups.com
For more options, visit this group at
http://cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---

Re: Access session in abstract class
country flaguser name
United States
2007-10-15 09:45:08

> I use a similar way to authenticate user identity.
> But I don't make the class "abstract" .  I
can't tell the difference
> between abstract or not  clearly.

Abstract class is a class which must be inherited. You can
not create
objects of the class. The only thing you can do with such a
class is
to inherit it in another class

> So my code is :
> public class SecureBase:Page {}
> But it works fine.

It would be very helpful if you could post the code in your
class. Are
you sure you are accessing Session in your class, if so,
paying a look
to it will help me find out what is wrong with my code

> I don't find any text about 'state'/'session' in my
web.config .
> So I think this might not be the problem of
configuration file or page
> directive.

It is not necessary that you've got all the settings in
web.config. If
your machine.config file has these directives, they are
inherited. In
that case, you will not see any special directives in
web.config file.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "DotNetDevelopment, VB.NET, C# .NET,
ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting"
group.
To post to this group, send email to DotNetDevelopmentgooglegroups.com
To unsubscribe from this group, send email to
DotNetDevelopment-unsubscribegooglegroups.com
For more options, visit this group at
http:
//cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---


[1-7]

about | contact  Other archives ( Real Estate discussion Medical topics )