How to access session variable in global.asa file - asp-classic

I am handling a classic asp code which is already developed.
We are initializing session variable on login page and accessing it in session_onstart in global.asa file to access data from the database.
I am getting an session value as undefined.
Sorry for this stupid question but is there any way to tackle this situation?

Related

Session is Null (No session at all) in class, but session does exist when accessing from aspx page

I am working on a multi developer web app. I am trying to sort out an issue that came up with one dev not being able to access session variables in his custom classes.
I synced his changes, and the same issue happens on my dev machine. (I.e., its not IIS specific)
Upon further inspection, the session object completely disappears (there is no session, nto even an empty session with a session ID. Any attempt to access HttpContext.Current.Session throws a null reference exception.
Running the page code again after setting some session variables, shows the variable are all saved and acting normal in the session that I can access from the page itself . (The session object behaves as it normally does)
So to be clear, in the same process, debugging from page through to custom class, the session object is accessible in the page, not existing in custom class, and when returning to the page, its available again.
I have tried setting just a normal string session var to eliminate possible problems with my variable (object) stored in the session. The same issue persists.
Any ideas?
Alrighty then.. always helps to bounce it off a wall :-)
We were using a textbox autocomplete extender that was referencing our own custom webmethod, (inside our app in a custom class). It seem the webmethod decoration forced it to operate stateless... hence no session.
We moved this webmethod decorated function into our page-codebehind page, and it now calls the other custom classes "with" session object availability.

Global.asax page ? In Asp.net?

In web application [asp.net] shall we write code that can retrive data from datbase or insert something in database. in any event of global.asax. when i am writing code in "Application_BeginRequest" even like :
Session["abc"]= 10; it is throwing exception that session can't declare some thing.Session state is not available in this context.
Oh. You need to read about ASP.NET page cycle
Then You will realize that Session state is not available on BeginRequest
Assuming I am understanding your question correctly then you are right that you cannot access session in that event. Try instead putting it in Application.AcquireRequestState. This is where ASP.NET sets up session and such like.
What you can use in BeginRequest is the Context which is acceesible in that request if you just need to set up data for just that request.

Preserve Session Variables Across HttpHandlers

I have an ASP.NET application with 5 .ashx HTTPHandlers that implement IRequiresSessionState or IReadOnlySessionState.
Upon calling the first handler I create a variable and store it in the session.
When I call the next HttpHandler the variable is not available in the session object.
I am use context.Session.
I have a global.asax where I retrieve the sessionId.
Is there a way to preserve session variables across HttpHandlers or does each Handler get its own session?
HeartAttack has a good discussion of this here:
http://weblogs.asp.net/ashicmahtab/archive/2008/09/18/how-to-use-session-values-in-an-httphandler.aspx
Basically, you need to do something like:
public class MyHandler:IHttpHandler,IRequiresSessionState
HTH's.
This should maintain session state across handlers. That is, when you log in, you only have one session for all of your requests into that domain. I'd suggest verifying that you are really logged in. Cache of course will work because that is common to all sessions but much more expensive on the server because it does not easily go away.

Session in asp.net

I need a detailed example about session in asp.net using c#
For example(using session in log in operation
The Session is the system in ASP.Net where you can save objects and variables User-based, so these items can be available across postbacks.
Adding a variable to the Session:
Session["key"] = myVar;
Retrieving a variable
myVar = Session["key"];
myVar = (MyType) Session["key"];
In the Session, you can save any .NET Framework type, But you should be very aware of the impact this design can weigh in your application, as this imposes scalability issues.
In the answer to this question there is an excellent utility class that can help you to abstract the session object, also take a look at it.
Sorry, not entirely sure what you're after with the login stuff, but using session is fairly trivial, the following two pages from MSDN should get you started:
How to: Save Values in Session State
How to: Read Values From Session State
If you've got the standard login controls on a page you can handle the LoggedIn Event to store additional details in the users Session State.
You might consider using Forms Authentication: http://msdn.microsoft.com/en-us/library/aa480476.aspx

Cookies and Objects

I'm having trouble figuring out how to access a cookie from a compiled object. I'm trying to make a compiled (DLL) object that will check the users cookie and then compare that to a database to confirm they have the correct access.
I can pass in the cookie info fine and the component will work, but I'm trying to have the component check the users cookie as well. I'm not even sure what object to use. I've been searching all weekend and I've seen references to httprequest, httpcookie, cookie, and cookiecollection.
I can look up cookie values on the page itself using Request.Cookies("inet")("user_id") but this doesn't work in the component.
Objects (App_Code/ compiled dlls) can only access Request via the static HttpContext.Current object
HttpCookie cookie = HttpContext.Current.Request.Cookies["CookieName"];
(If it's not called from a web app, HttpContext.Current is null, so you may want to check for that when running in unit testing)
(If this isn't App_Code, you'll need to reference System.Web)
If the component is a separate DLL from your web app you'd need to pass in a reference to the Request object.
That said why not just read/check the cookie value in your ASP.NET code before calling into your DLL. It's not such a good idea to have your business logic coupled to your web tier like this.

Resources