Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session Error in Global.asax?

Status
Not open for further replies.

NoCoolHandle

Programmer
Apr 10, 2003
2,321
US
Ok, I have been playing around with different security options and in the process bumped into an error that is confusing me. (error below)

In the Global.asax file I have some code that runs everytime a page is loaded. The thing that is wierd is that I have found that I can't read or set Sesson Variables....

This seems to be counter to anything I have seen before (at least in traditional asp)

any ideas??

TIA
Rob

Session state is not available in this context.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Session state is not available in this context.

Source Error:


Line 30:
Line 31: Private Sub LoadFromSession()
Line 32: session("y")="y"

Source File: C:\Inetpub\ Line: 32

Stack Trace:


[HttpException (0x80004005): Session state is not available in this context.]
System.Web.HttpApplication.get_Session() +99
ASP.Global_asax.LoadFromSession() in C:\Inetpub\ ASP.Global_asax.global_authenticateRequest(Object sender, EventArgs e) in C:\Inetpub\ System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +76
 
OK... Sorted it out. The problme (was I didn't go to google first) related to the event that was causing the code to run.

The Global_asax.global_authenticateRequest event runs before any session state exists as asp still hasn't authenticated the user.

The Soution.. Use the Cache object instead (and remember to clean it up on session_end

e.g.
SETTING THE CACHE
HttpContext.Current.Cache.Item("Principal") = HttpContext.Current.User

READING THE CACHE

HttpContext.Current.User = CType(HttpContext.Current.Cache.Item("Principal"), System.Security.Principal.IPrincipal)

AND CLEARING..
Just loop and clear the elements... normal collection type stuff.

Seems to work anywhere you might otherwise use a session object.


HTH

Rob

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top