473,569 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using PostAcquireRequ estState 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 PostAcquireRequ estState 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_Pos tAcquireRequest State(object sender,
EventArgs e)
{
if (!clsWebFunctio ns.IsLoggedIn(S ession) &&
(!Request.Url.T oString().Conta ins("HomePageUR L")
{
Session["requestedpage" = Request.Url.ToS tring();
}
}

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 7618
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
IRequiresSessio nState or IReadOnlySessio nState interfaces.

You can check HttpContext.Cur rent.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**********@y ahoo.com.auwrot e in message
news:96******** *************** ***********@j33 g2000pri.google groups.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 PostAcquireRequ estState 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_Pos tAcquireRequest State(object sender,
EventArgs e)
{
if (!clsWebFunctio ns.IsLoggedIn(S ession) &&
(!Request.Url.T oString().Conta ins("HomePageUR L")
{
Session["requestedpage" = Request.Url.ToS tring();
}
}

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.Cur rent.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 ---

//IRequiresSessio nState: (MSDN) Specifies that the target HTTP
handler requires read and write access to session-state values.
bool RequiresSession =
clsGlobalFuncti ons.ClassHasInt erface(
Context.Current Handler.GetType (),
typeof(global:: System.Web.Sess ionState.IRequi resSessionState ));

if (RequiresSessio n)
{
string RequestURL = Request.Url.ToS tring().ToLower ();
if (
!clsWebFunction s.IsLoggedIn(Se ssion) &&
!( // Don't create session variable if
navigating to one of the following pages.

RequestURL.Cont ains((clsWebFun ctions.URLConst ants(eURL.HomeP ageURL)).ToLowe r().Replace("~" ,
""))
||
RequestURL.Cont ains((clsWebFun ctions.URLConst ants(eURL.EnRou teLoginURL)).To Lower().Replace ("~",
""))
||
RequestURL.Cont ains((clsWebFun ctions.URLConst ants(eURL.Defau lt)).ToLower(). Replace("~",
""))
)
)
{
Session[eSession.Reques tedPage.ToStrin g()] =
Request.Url.ToS tring();

Response.Redire ct(clsWebFuncti ons.URLConstant s(eURL.EnRouteL oginURL));
}
}
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**********@y ahoo.com.auwrot e in message
news:27******** *************** ***********@w5g 2000prd.googleg roups.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 ---

//IRequiresSessio nState: (MSDN) Specifies that the target HTTP
handler requires read and write access to session-state values.
bool RequiresSession =
clsGlobalFuncti ons.ClassHasInt erface(
Context.Current Handler.GetType (),
typeof(global:: System.Web.Sess ionState.IRequi resSessionState ));

if (RequiresSessio n)
{
string RequestURL = Request.Url.ToS tring().ToLower ();
if (
!clsWebFunction s.IsLoggedIn(Se ssion) &&
!( // Don't create session variable if
navigating to one of the following pages.

RequestURL.Cont ains((clsWebFun ctions.URLConst ants(eURL.HomeP ageURL)).ToLowe r().Replace("~" ,
""))
||
RequestURL.Cont ains((clsWebFun ctions.URLConst ants(eURL.EnRou teLoginURL)).To Lower().Replace ("~",
""))
||
RequestURL.Cont ains((clsWebFun ctions.URLConst ants(eURL.Defau lt)).ToLower(). Replace("~",
""))
)
)
{
Session[eSession.Reques tedPage.ToStrin g()] =
Request.Url.ToS tring();

Response.Redire ct(clsWebFuncti ons.URLConstant s(eURL.EnRouteL oginURL));
}
}


Jun 27 '08 #5

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

Similar topics

12
2389
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 Visual Basic Projects folder. When I select the ASP.Net Application icon and type in http://localhost/MyWebCalculator, I recieve an error in a...
22
3753
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 manipulate them. It starts > and ends with <script></script> tags. > > Yours looks like a compiled version of it.
12
3807
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 (the oleDBConnection on global.asa.vb) at the other aspx pages ?
8
4853
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 differences in the way both the objects are handled by IIS. Are both objects stored in different memory spaces? I can access both the objects in my web...
1
3274
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 probably more common than HttpHandlers, but could still stand to be advertised a bit more. HttpModules are incredibly easy to explain, so this will...
4
3025
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 debugger unless I add a Global.asax file. When I do that and then try to run the debugger I receive error 403. If I remove the Global.asax file...
8
5768
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
5019
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: <%@ Application Codebehind="Global.asax.vb" Inherits="<Namespace>." %> I've done a lot of things I've found on the web to no avial, but here are some...
15
2538
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 Global class. Note: I use these shared variables for read only values that are set at application start. It would appear the 2.0 doesn't like you...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2115
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 we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.