views:

276

answers:

4

The Problem

In the stack that we re-use between projects, we are putting a little bit too much data in the session for passing data between pages. This was good in theory because it prevents tampering, replay attacks, and so on, but it creates as many problems as it solves.

Session loss itself is an issue, although it's mostly handled by implementing Session State Server (or by using SQL Server). More importantly, it's tricky to make the back button work correctly, and it's also extra work to create a situation where a user can, say, open the same screen in three tabs to work on different records.

And that's just the tip of the iceberg.

There are workarounds for most of these issues, but as I grind away, all this friction gives me the feeling that passing data between pages using session is the wrong direction.

What I really want to do here is come up with a best practice that my shop can use all the time for passing data between pages, and then, for new apps, replace key parts of our stack that currently rely on Session.

It would also be nice if the final solution did not result in mountains of boilerplate plumbing code.

Proposed Solutions

Session

As mentioned above, leaning heavily on Session seems like a good idea, but it breaks the back button and causes some other problems.

There may be ways to get around all the problems, but it seems like a lot of extra work.

One thing that's very nice about using session is the fact that tampering is just not an issue. Compared to passing everything via the unencrypted QueryString, you end up writing much less guard code.

Cross-Page Posting

In truth I've barely considered this option. I have a problem with how tightly coupled it makes the pages -- if I start doing PreviousPage.FindControl("SomeTextBox"), that seems like a maintenance problem if I ever want to get to this page from another page that maybe does not have a control called SomeTextBox.

It seems limited in other ways as well. Maybe I want to get to the page via a link, for instance.

QueryString

I'm currently leaning towards this strategy, like in the olden days. But I probably want my QueryString to be encrypted to make it harder to tamper with, and I would like to handle the problem of replay attacks as well.

On 4 guys from Rolla, there's an article about this.

However, it should be possible to create an HttpModule that takes care of all this and removes all the encryption sausage-making from the page. Sure enough, Mad Kristenson has an article where he released one. However, the comments make it sound like it has problems with extremely common scenarios.

Other Options

Of course this is not an exaustive look at the options, but rather the main options I'm considering. This link contains a more complete list. The ones I didn't mention such as Cookies and the Cache not appropriate for the purpose of passing data between pages.

In Closing...

So, how are you handling the problem of passing data between pages? What hidden gotchas did you have to work around, and are there any pre-existing tools around this that solve them all flawlessly? Do you feel like you've got a solution that you're completely happy with?

Thanks in advance!

Update: Just in case I'm not being clear enough, by 'passing data between pages' I'm talking about, for instance, passing a CustomerID key from a CustomerSearch.aspx page to Customers.aspx, where the Customer will be opened and editing can occur.

+2  A: 

I would never do this. I have never had any issues storing all session data in the database, loading it based on the users cookie. It's a session as far as anything is concerned, but I maintain control over it. Don't give up control of your session data to your web server...

With a little work, you can support sub sessions, and allow multi-tasking in different tabs/windows.

Fosco
So just to be clear, you *are* using the session to pass data between pages? So for instance, if you had a crud app with a Customers page, you might set session("CustomerID")=5 and then do Response.Redirect("Customers.aspx")?
Brian MacKay
Nope. I am using a cookie, to match against the database, to load the related data. I don't use the provided Session functionality of ASP or .NET or PHP, etc.. I use a class (called Session, hah) and the constructor will try to match the users cookie with an existing session, or make a new one. The data is then linkable from there...
Fosco
Ok, that's interesting, I can see how you could build a lot of extra goodness in that way. Ultimately though, you do use your homebrew session object to do the basic steps mentioned above, right?
Brian MacKay
Yeah. It gives you the freedom to do whatever you want... I highly recommend looking into it.
Fosco
@Fosco - What is your class doing that the normal session class is not? ASP.NET's session allows you to persist it to a database if you want, but if you do not want that, you can persist in memory on the web server and get better performance.
Thomas
@Fosco- Btw, all the problems about the back button and stuffing large amounts of data into session are still true even if you roll your own session class.
Thomas
@Thomas - The back button wasn't mentioned in this question, and I'm not sure that one could ever be solved 100%. In the past I have implemented one-use form tokens to eliminate any double postings, if that helps at all... As far as large amounts of data, I would have to disagree with you.
Fosco
@Fosco - The back button is mentioned in the second paragraph of the OP. If you stuff a huge amount of data into session, using a session server, how is that any different than rolling your own session class storing to a database where you stuff a huge amount of data into session? There are reasons for rolling your own such as with ASP and probably PHP (not sure) but ASP.NET's Session combined with ViewState is actually a pretty good combination out of the box if used properly.
Thomas
@Thomas I'm curious about how you handle passing data between pages -- maybe you could post an answer? I've re-named the question to be more about best practices for passing data between pages, since that's really what I'm after.
Brian MacKay
+1  A: 

As a starting point, I find using the critical data elements, such as a Customer ID, best put into the query string for processing. You can easily track/filter bad data coming off of these elements, and it also allows for some integration with e-mail or other related sites/applications.

In a previous application, the only way to view an employee or a request record involving them was to log into the application, do a search for the employee or do a search for recent records to find the record in question. This became problematic and a big time sink when somebody from a related department needed to do a simple view on records for auditing purposes.

In the rewrite, I made both the employee Id, and request Ids available through a basic URL of "ViewEmployee.aspx?Id=XXX" and "ViewRequest.aspx?Id=XXX". The application was setup to A) filter out bad Ids and B) authenticate and authorize the user before allowing them to these pages. What this allowed the primarily application users to do was to send simple e-mails to the auditors with a URL in the e-mail. When they were in a big hurry, they were in their bulk processing time, they were able to simply click down a list of URLs and do the appropriate processing.

Other session related data, such as modification dates and maintaining the "state" of the user's interaction with the application gets a little more complex, but hopefully this provides a starting poing for you.

Dillie-O
+3  A: 

First, the problems with which you are dealing relate to handling state in a state-less environment. The struggles you are having are not new and it is probably one of the things that makes web development harder than windows development or the development of an executable.

With respect to web development, you have five choices, as far as I'm aware, for handling user-specific state which can all be used in combination with each other. You will find that no one solution works for everything. Instead, you need to determine when to use each solution:

  • Query string - Query strings are good for passing pointers to data (e.g. primary key values) or state values. Query strings by themselves should not be assumed to be secure even if encrypted because of replay. In addition, some browsers have a limit on the length of the url. However, query strings have some advantages such as that they can be bookmarked and emailed to people and are inherently stateless if not used with anything else.

  • Cookies - Cookies are good for storing very tiny amounts of information for a particular user. The problem is that cookies also have a size limitation after which it will simply truncate the data so you have to be careful with putting custom data in a cookie. In addition, users can kill cookies or stop their use (although that would prevent use of standard Session as well). Similar to query strings, cookies are better, IMO, for pointers to data than for the data itself unless the data is tiny.

  • Form data - Form data can take quite a bit of information however at the cost of post times and in some cases reload times. ASP.NET's ViewState uses hidden form variables to maintain information. Passing data between pages using something like ViewState has the advantage of working nicer with the back button but can easily create ginormous pages which slow down the experience for the user. In general, ASP.NET model does not work on cross page posting (although it is possible) but instead works on posts back to the same page and from there navigating to the next page.

  • Session - Session is good for information that relates to a process with which the user is progressing or for general settings. You can store quite a bit of information into session at the cost of server memory or load times from the databases. Conceptually, Session works by loading the entire wad of data for the user all at once either from memory or from a state server. That means that if you have a very large set of data you probably do not want to put it into session. Session can create some back button problems which must be weighed against what the user is actually trying to accomplish. In general you will find that the back button can be the bane of the web developer.

  • Database - The last solution (which again can be used in combination with others) is that you store the information in the database in its appropriate schema with a column that indicates the state of the item. For example, if you were handling the creation of an order, you could store the order in the Order table with a "state" column that determines whether it was a real order or not. You would store the order identifier in the query string or session. The web site would continue to write data into the table to update the various parts and child items until eventually the user is able to declare that they are done and the order's state is marked as being a real order. This can complicate reports and queries in that they all need to differentiate "real" items from ones that are in process.

One of the items mentioned in your later link was Application Cache. I wouldn't consider this to be user-specific since it is application wide. (It can obviously be shoe-horned into being user-specific but I wouldn't recommend that either). I've never played with storing data in the HttpContext outside of passing it to a handler or module but I'd be skeptical that it was any different than the above mentioned solutions.

In general, there is no one solution to rule them all. The best approach is to assume on each page that the user could have navigated to that page from anywhere (as opposed to assuming they got there by using a link on another page). If you do that, back button issues become easier to handle (although still a pain). In my development, I use the first four extensively and on occasion resort to the last solution when the need calls for it.

Thomas
Hi Thomas, thanks for the answer. I accepted sw12345's answer because I wasn't so much looking for the pros and cons of the various options as some advanced insight into tooling that can help solve this, especially tooling that could help me automate querystring encryption in a transparent way.
Brian MacKay
+3  A: 

Alright, so I want to preface my answer with this; Thomas clearly has the most accurate and comprehensive answer so far for people starting fresh. This answer isn't in the same vein at all. My answer is coming from a "business developer's" standpoint. As we all know too well; sometimes it's just not feasible to spend money re-writing something that already exists and "works"... at least not all in one shot. Sometimes it's best to implement a solution which will let you migrate to a better alternative over time.

The only thing I'd say Thomas is missing is; client-side javascript state. Where I work we've found customers are coming to expect "Web 2.0"-type applications more and more. We've also found these sorts of applications typically result in much higher user satisfaction. With a little practice, and the help of some really great javascript libraries like jQuery (we've even started using GWT and found it to be AWESOME) communicating with JSON-based REST services implemented in WCF can be trivial. This approach also provides a very nice way to start moving towards a SOA-based architecture, and clean separation of UI and business logic.

But I digress.

It sounds to me as though you already have an application, and you've already stretched the limits of ASP.NET's built-in session state management. So... here's my suggestion (assuming you've already tried ASP.NET's out-of-process session management, which scales signifigantly better than the in-process/on-box session management, and it sounds like you have because you mentioned it); NCache.

NCache provides you with a "drop-in" replacement for ASP.NET's session management options. It's super easy to implement, and could "band-aid" your application more than well enough to get you through - without any significant investment in refactoring your existing codebase immediately.

You can use the extra time and money to start reducing your technical debt by focusing new development on things with immediate business-value - using a new approach (such as any of the alternatives offered in the other answers, or mine).

Just my thoughts.

Steve
sw12345: He sw, thanks for the thoughtful answer. I accepted it because it really is more the kind of answer I'm looking for -- Thomas's overview of the various strategies is comprehensive, but I already knew that stuff and event mentioned a lot of it in my actual question. I wanted more advanced strategies to look into. Thanks!
Brian MacKay