473,505 Members | 16,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using PostAcquireRequestState in global.asax

Hi,

I have the following situation. If a user browses to http://domain/somemiscURL.aspx
without going through the front door I need to catch the request,
forward them to a login page and then, on completion, send them to the
original page that they requested.

My 'plan' was to do the following.
1/ Catch every request and test if it's "logged in" (determined by
existence of an object help in the Session
2/ If they fail, store the path that they requested in a session state
then redirect them to the login page
3/ On succesfull login, check the session variable of their original
request and send them on their way.

I've found the PostAcquireRequestState event on the httpApplication
object which, according to MSDN "Occurs when the request state (for
example, session state) that is associated with the current request
has been obtained".

From global.asax (simplified)

protected void Application_PostAcquireRequestState(object sender,
EventArgs e)
{
if (!clsWebFunctions.IsLoggedIn(Session) &&
(!Request.Url.ToString().Contains("HomePageURL")
{
Session["requestedpage"= Request.Url.ToString();
}
}

The issue is that I keep getting a "Session state is not available in
this context." error (on the call to IsLoggedIn).

Can someone please point me in the right direction?

Thanks in advance for your help,
Damien

Jun 27 '08 #1
4 7596
Hi,

it is also raised for all HTTP handlers (Page is just one of them, there is
for example WebResource handler in ASP.NET, too) and not all of them require
session state. It is determined by if the current HTTP handler implements
IRequiresSessionState or IReadOnlySessionState interfaces.

You can check HttpContext.Current.Handler if it implements either of these
interfaces, when it is safe to access the session state (depending do you
also need to write to the session or just read it)

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

<da**********@yahoo.com.auwrote in message
news:96**********************************@j33g2000 pri.googlegroups.com...
Hi,

I have the following situation. If a user browses to
http://domain/somemiscURL.aspx
without going through the front door I need to catch the request,
forward them to a login page and then, on completion, send them to the
original page that they requested.

My 'plan' was to do the following.
1/ Catch every request and test if it's "logged in" (determined by
existence of an object help in the Session
2/ If they fail, store the path that they requested in a session state
then redirect them to the login page
3/ On succesfull login, check the session variable of their original
request and send them on their way.

I've found the PostAcquireRequestState event on the httpApplication
object which, according to MSDN "Occurs when the request state (for
example, session state) that is associated with the current request
has been obtained".

From global.asax (simplified)

protected void Application_PostAcquireRequestState(object sender,
EventArgs e)
{
if (!clsWebFunctions.IsLoggedIn(Session) &&
(!Request.Url.ToString().Contains("HomePageURL")
{
Session["requestedpage"= Request.Url.ToString();
}
}

The issue is that I keep getting a "Session state is not available in
this context." error (on the call to IsLoggedIn).

Can someone please point me in the right direction?

Thanks in advance for your help,
Damien

Jun 27 '08 #2
Thanks very much Teemu, that's a big help.
You can check HttpContext.Current.Handler if it implements either of these
interfaces, when it is safe to access the session state (depending do you
also need to write to the session or just read it)
I actually need to write to it. Is that going to be possible?
Damien
Jun 27 '08 #3
Hi again,

I implement this check and the failures have disapeared. The code
works perfectly - on Firefox, however for some unknown reason, not on
IE7.

By examinine the trace information, when the page is accessed via IE,
the session variable is not created. As this is all "server side", I'm
stuck as to why this would be.

Thanks in advance for any help.
DS
--- code extract below ---

//IRequiresSessionState: (MSDN) Specifies that the target HTTP
handler requires read and write access to session-state values.
bool RequiresSession =
clsGlobalFunctions.ClassHasInterface(
Context.CurrentHandler.GetType(),
typeof(global::System.Web.SessionState.IRequiresSe ssionState));

if (RequiresSession)
{
string RequestURL = Request.Url.ToString().ToLower();
if (
!clsWebFunctions.IsLoggedIn(Session) &&
!( // Don't create session variable if
navigating to one of the following pages.

RequestURL.Contains((clsWebFunctions.URLConstants( eURL.HomePageURL)).ToLower().Replace("~",
""))
||
RequestURL.Contains((clsWebFunctions.URLConstants( eURL.EnRouteLoginURL)).ToLower().Replace("~",
""))
||
RequestURL.Contains((clsWebFunctions.URLConstants( eURL.Default)).ToLower().Replace("~",
""))
)
)
{
Session[eSession.RequestedPage.ToString()] =
Request.Url.ToString();

Response.Redirect(clsWebFunctions.URLConstants(eUR L.EnRouteLoginURL));
}
}
Jun 27 '08 #4
Hi,

Can you confirm with some test code that is Session property null in that
case?

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
<da**********@yahoo.com.auwrote in message
news:27**********************************@w5g2000p rd.googlegroups.com...
Hi again,

I implement this check and the failures have disapeared. The code
works perfectly - on Firefox, however for some unknown reason, not on
IE7.

By examinine the trace information, when the page is accessed via IE,
the session variable is not created. As this is all "server side", I'm
stuck as to why this would be.

Thanks in advance for any help.
DS
--- code extract below ---

//IRequiresSessionState: (MSDN) Specifies that the target HTTP
handler requires read and write access to session-state values.
bool RequiresSession =
clsGlobalFunctions.ClassHasInterface(
Context.CurrentHandler.GetType(),
typeof(global::System.Web.SessionState.IRequiresSe ssionState));

if (RequiresSession)
{
string RequestURL = Request.Url.ToString().ToLower();
if (
!clsWebFunctions.IsLoggedIn(Session) &&
!( // Don't create session variable if
navigating to one of the following pages.

RequestURL.Contains((clsWebFunctions.URLConstants( eURL.HomePageURL)).ToLower().Replace("~",
""))
||
RequestURL.Contains((clsWebFunctions.URLConstants( eURL.EnRouteLoginURL)).ToLower().Replace("~",
""))
||
RequestURL.Contains((clsWebFunctions.URLConstants( eURL.Default)).ToLower().Replace("~",
""))
)
)
{
Session[eSession.RequestedPage.ToString()] =
Request.Url.ToString();

Response.Redirect(clsWebFunctions.URLConstants(eUR L.EnRouteLoginURL));
}
}


Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2377
by: Luther Hert | last post by:
While trying to work through textbook lessons for Vb.net Step by Step,Version 2003, Chapter 22, the first step is to create a new Web application Project,using the ASP.Net Application icon in the...
22
3727
by: fd123456 | last post by:
Hi Tom ! Sorry about the messy quoting, Google is playing tricks on me at the moment. > Global.asax is where you normally have the Global Application > and Session variables and code to...
12
3798
by: John M | last post by:
Hello, On Microsoft Visual Studio .NET 2003, I want to use some global elements, that can be used in each one of my pages. i.e I put a oleDBConnection on global.asax.vb How can I use it...
8
4845
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
1
3268
by: Anonieko | last post by:
Global.asax? Use HttpModules Instead! In a previous post, I talked about HttpHandlers - an underused but incredibly useful feature of ASP.NET. Today I want to talk about HttpModules, which are...
4
3019
by: Al Santino | last post by:
Hello, I've created a simple C# web services project using Visual Studio 2005. My service compiles and runs correctly when called by remote clients. I'm able to step through the service in the...
8
5764
by: Victor | last post by:
Can I get the events in GLOBAL.ASAX to fire if a classic ASP page is being accessed by the user?
16
5010
by: thefritz_j | last post by:
We just converted our VS2003 1.1 VB web project (which was working fine) to VS2005 2.0 and now I get: Parser Error Message: Could not load type '<Namespace>.'. Source Error: Line 1: <%@...
15
2527
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
0
7307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.