I'm having issues with my web-application expiring the session too soon (not unexpected, just too soon for the type of system I am developing). My host doesn't allow changing the session expiration time.
So I was thinking, is there any way that I can (in ASP .NET MVC) for instance save my session variables in a form, so that when that form is somehow submitted or the page has changed, I can reset the session variables?
But with the above theory (if it's even a good theory), then how do I handle such stuff in my controllers? Or is there a better way to reach a session that is theoretically infinite?
What you are describing is the standard use-case for cookies.
If your variables don't need to be secure, then save each in a cookie (or, if you want to do it right, have a "session object" that you serialize in a cookie).
If your variables need to be secure, save them in a database or flat file and save the encrypted row id in the cookie.
You cannot retrieve a session. Once gone, it is gone (and that's good for security reasons).
However, you can keep the session from expiring:
http://weblogs.asp.net/stevewellens/archive/2009/06/09/ah-ah-ah-ah-staying-alive-staying-alive.aspx
Related
I am developing a asp.net web application.
I am making use of Session variable to store user id and user name.
Session variables tend to expire after some time, which we can configure in web.config.
To avoid session variable from getting expired is it good if we re assign the session variable to itself every 5-10 minutes!?
in the client side if we have a settimeout or setinterval and make a ajax call to assign the session variable to itself every five minutes, will this prevent session expiring!?
I want to do this to make sure that no error arises because of session expiring.
You basically have two options to extend default session expiry time or to do what you’re suggesting with reassigning values.
Why don’t you just extend default session expiry time? It will use more memory on the server but unless you have a ton of users and a lot of data stored in session objects (in which case you should consider getting better server for you app) you should be fine.
Another thing I’d use is some kind of analytics software to tell me how much time on average your users are spending with the app. This will give you good info on what do to.
So I am new to the whole datacache thing. I am building an app for a client who has potential to grow substantially so I am using the datacache instead of Session variables so that I can have multiple servers.
I have been in development mode and this has worked just fine, but it has just been me.
Now we are testing and completely new people on a computer that have never been to the site before are being recognized as another user somehow...
I do use cookies to remember a user, but these guys are coming in for the first time so that can't be it. Never had this problem with Session variables so I must be doing something wrong with the datacache.
Why does the datacache confuse the users? How do I prevent this?
Thanks!
David
cache = server level, session = user level. Your server is saving the cache data and passing it along to the users. If you have intentions of having multiple users connected, and storing separate data for each, sessions is actually the correct way to do it.
As for performance, yes there will be a slight performance hit, but it shouldn't be too drastic unless you've got a massive amount of users hitting the site at one time or you're storing huge amounts of data to the session.
You need to use Session to store a logged in users credentials and other session specific information. Datacache is used to store application wide data that can be quickly accessed, things like xml files used on the website or perhaps a dataset used by anyone on the website. Session is a unique id and "thread" for each user for their specific user name and all other information related to their logged in id.
i am using a session cookie (not a permanent one) to save the user id to know if the user is logged in.
basically, user logs in, we check the credentials, then set a session cookie userID = 37 (for this particular user, another user would have 73 or 69, etc...)
Session.Add("UserID", 37);
my question is, is it possible for the logged in user to somehow change this session cookie from 37 to 73 and thus fool the server into thinking he is actually user 73?
if YES, then what am i doing wrong, how to handle this case? it seems insane to put in session user id and password hash and check them EVERY TIME??
we are using this userid value also in queries later to restrict them.
i am sorry if this is not an EXACT code question, but it is very much relevant to my code.
The session cookie contains only the session id. It is used to identify the user. It contains nothing more. The actual information for this session is stored on the server. So this is secure. The user can never change the value that has been stored on the server. The user cannot change his id if you stored this inside the session.
This being said, when dealing with user ids you could consider using forms authentication to track authenticated users instead of reinventing wheels with the Session.
ASP.NET session state provides an important security advantage over client state management techniques in that the actual state is stored on the server side and not exposed on the client and other network entities along the HTTP request path. However, there are several important aspects of session state operation that need to be considered in order to maintain application security. Security best practices fall into three major categories: preventing session ID spoofing and injection, securing the state storage in the back-end, and ensuring session state deployment security in dedicated or shared environments.
Read : Securing Session State
That isn't the cookie, and is perfectly safe as it cannot be changed by the user. The only thing stored on the server side in a cookie is the session ID.
As the other answers have noted, the actual value (37 in the example) is stored on the server, not the client, but that doesn't mean that you're immune to potential attacks. This mechanism is still vulnerable to cross site scripting attacks. Basically, what is stored on the client's cookie is some big long identifier. If someone other than the actual user gets ahold of that identifier they can put that in a cookie of their own and essentially pretend to be that user. You can research cross site scripting more on your own (I'm not an expert on the subject) to see some of the common ways that a malicious user will attempt to look at other users' cookies and to try to set it as their own, along with ways of defending against such attacks (some of which I'm sure will be done for you by browsers and ASP).
I am creating a website on ASP.Net in which a user logs on and I need to store the user specific data (plus some extra data) somewhere so that all the pages in my website can readily access the data.
At one time many users could be accessing the website and every user will have his own specific data.
Currently I am using sessions to store the data after login and accessing the data on different pages as and when needed. I am also using session to pass data from one page to another. I also don't want to use cookies as many companies don't allow cookies to be created.
I understand that this is not the best practice. Can you guys suggest what would be the best way to manage the data?
Thanks,
Abhi.
Sessions are not necessarily a bad way to go. Ensure that data kept in the session is as concise as possible, and that your application will support all environments that it may be deployed into.
Also remember that Sessions do not provide data persistance once the session has been expired, so if you require data persistence, then a database would be more suitable.
This is what MS says about it, http://msdn.microsoft.com/en-us/library/ms178581.aspx:
"SessionID values are sent in clear text, whether as a cookie or as part of the URL. A malicious user could get access to the session of another user by obtaining the SessionID value and including it in requests to the server. If you are storing sensitive information in session state, it is recommended that you use SSL to encrypt any communication between the browser and server that includes the SessionID value."
As long as you use a save connection you should be fine
ASP.NET already provides you with information about the user through the IPrincipal interface and the User property.
If you need extra information about each user, you can use these to implement a User Context.
what is your preferred method to maintain state of an asp.net page, if it is a public website (involving shopping cart, wish-list etc). I am in the process of designing a website that will need to ensure that the user is not able to tamper with the state (such as delete cookies etc).
Both of those pieces of data (shopping cart & wish list) sound like they should be stored in your database, so they can persist beyond cookies being deleted or the session timing out.
To prevent user tampering you will need to store session state on the server side. A good practice is store it either in a database (sql server) or out of process, which can be either on the same server or another server, sometimes called a state server.
I am in the process of designing a
website that will need to ensure that
the user is not able to tamper with
the state (such as delete cookies
etc).
I would use the session state. It's easy and effective.
You can't stop them from tampering with it as far as deleting cookies etc. Maybe altering the cookies, but not deleting them.
You can store the vital information on the server, so no sensitive information is stored on the client in a cookie etc.
The preferred method of maintaining state is using viewstate. However, you want to keep the amount you store in it to a minimum as it will noticeably affect speed.
If you need to store information from page to page, I would recommend using session state. It's easy and very flexible.
The user will always be able to tamper with any method you use to store state. Consider these examples:
Cookies - can alter, delete
Session - can delete, timeout, change session key
ViewState - can mess with the key
I encrypt my data and store the key in a cookie - such as cart id, user id, etc.. The cookie can be loaded at login and can be tampered with, but because the key encrypted, no real harm can be done. Storing in the session can work, but the timeouts have bitten me on many occasions and requires more considerations.