Session State and Security
Session state control is a tough thing to handle. If you are in a web farm environment it gets even tougher. What are the options?
Session Key Management
So, how would you do this? Most developers will generate a unique key and pass it back and forth, or maybe even use the ViewState. I like to use both.
When trying to use manual session keys, I will generate the key and then append it to all internal links plus add it to the ViewState for post-backs. This is that hard, just have an extra function that is called last iterate and append the data to all hyperlink objects (doesn't work it you use HTML href's). But is this secure?
I came up with, IMHO, a method for securing the session key itself. Stay with me, it gets interesting. First off, you will need the following data:
- Use an IIS Session State server
- Don't use web farms and use the Session object
- Manage session state manually by passing a key
Session Key Management
So, how would you do this? Most developers will generate a unique key and pass it back and forth, or maybe even use the ViewState. I like to use both.
When trying to use manual session keys, I will generate the key and then append it to all internal links plus add it to the ViewState for post-backs. This is that hard, just have an extra function that is called last iterate and append the data to all hyperlink objects (doesn't work it you use HTML href's). But is this secure?
I came up with, IMHO, a method for securing the session key itself. Stay with me, it gets interesting. First off, you will need the following data:
- Browser User Agent (ServerVariables value)
- First three octets of client IP address (Parse the ServerVariables value)
- Unique user number (Probably from a database or server side xml file)
- Last accessed timestamp
- Upon first access, update last accessed timestamp
- Take all four values and append into one large string
- Use a hashing algorithm (I like SHA256) and hash the string
- Store the returned value (now your session key) and pass to client in ViewState and appended to links
- Take the passed in session key and validate it matches a current one in the database. Some developers also enforce a "session" timeout and check the last accessed time and log them out if it exceeds the system timeout. For our purposes, we will assume they have not reached system timeout.
- Now that it is valid, we ditch the entire key and RECREATE IT AS WE DID BEFORE. Read that again... we get all four of the ORIGINAL values, append them, and hash them. Then we validate against the database to see if they still match. This is the security we are looking for.
- Now that we are positive the keys match, we ditch the entire key again, update the last accessed timestamp, and create a new key and deliver it to the client the same way we did with the initial key.