views:

67

answers:

3

I have a user complaining about frequent timeouts in my Intranet web page. While looking for a solution I found this post:

http://forums.asp.net/t/152925.aspx?PageIndex=1

Where a poster recommends intercepting the redirect to the login page, submit the data to the database, then either reauthorize the user (without their knowledge) or redirect to login page. My goal is to avoid the situation where a user enters data in a form, walks away, then comes back to submit it, only to be told they have to login again (which is fine, if the data remained and the user was sent right back to the original webform).

Does anyone know how I can accomplish this on specific pages in my app (not all of them)?

A: 

Well, the easy way it to drastically lengthen the timeout specified in the web.config file.

Brad
@Brad: I should be able to do that, but for some reason this seems to have no effect. Also, I don't necessarily want this to happen on every page, just some of them.
MAW74656
+3  A: 

It's not necessarily trivial, but you can add an ajax component that makes occasional calls to a page to keep the session alive. This way you could lengthen the session for any particular page you need to without affecting the application as a whole.

EDIT

If you really want to let the session expire, but keep the form data, you can implement

protected void Application_PostAuthenticateRequest (object sender, EventArgs e)

event handler in your global.asax.cs file. This is called before the forms authentication redirect happens, and the form data is available to your application at this point, so you can persist it to whatever medium is necessary until your user is authenticated again. In addition, if you check the

((HttpApplication)sender).Request.Path

property it will tell you which page was requested.

arootbeer
+1, this seems very doable and exactly to point.
Brad
@arootbeer: Yes, I have heard of a similiar method. However, my real goal here is not to maintain the session, but the data. I consider vastly expanding the timeout to be a last resort. And what do you mean by "component"?
MAW74656
What component means depends on how your pages are built, but most likely it would be a tiny user control that has a Javascript method to occasionally call a URL you give it. As a user control you can just include it inline on the pages that need it.
arootbeer
As to maintaining the user's data, the fact that they're "losing" it is a result of ASP.Net not honoring sessions that have expired, not a problem with the application itself. They could likely work around it just by opening the login screen in a different window/tab, and logging in again before submitting their data.
arootbeer
I understand, but my question is whether or not it is possible to get around that. So, can I programmatically refresh the authentication and submit the data without the user having to jump through hoops?
MAW74656
Gotcha. I'll udpate my answer.
arootbeer
We have used this technique in a web app where we needed for specific pages to be able to extend the session timeout.
JMarsch
Ok, ok, that's what I'm talking about! Now, for a stupid question: How can I get to edit global.asax.cs? I've never modified this before.
MAW74656
If your application doesn't have one, you'll have to add it - `.asax file` is one of the specific types of files you can add to a web application from the Visual Studio "New File" dialog. It defaults to being named "Global.asax".
arootbeer
I changed the event handler from `Application_AuthenticateRequest` to `Application_PostAuthenticateRequest` because it appears to be the last place you can intercept the request before the redirect.
arootbeer
@arootbeer: OMG! I can do soooooo much with this! Unhandled exceptions, session start.... why isn't this included in a new project by default!??
MAW74656
Alright, so I can grab the data from controls in the original page, then redirect them to login page, then send them to the original page with the data as querystring or session variables.
MAW74656
Possibly. I'd think it would be difficult to do with session, since the login page will be creating a new session. But you can put it *somewhere*, which will let you do what you want. As to why it's not included by default, well, it is. If you create a new web application, Global.asax is one of the files that's included. Maybe it was removed from your application because it wasn't used?
arootbeer
No, I started the app from scratch, but since I've never used it before, I have no idea whether or not its been there. Your right, querystring would probably work better than session. I'll experiment and let you guys know!
MAW74656
@arootbeer: Can you give an example of how to reference the controls (textbox, dropdownlist) from the sending page in Global.asax?
MAW74656
You can't reference the controls; you'll have to deal with the `Form` data on the `sender`.
arootbeer
A: 

I'm going to try using cookies to preserve the data. My plan is to update the user's cookie after each control is changed, then add logic to the page_load property of the page to populate the form data after the user is logged back in.

MAW74656