views:

72

answers:

3

I am building a asp.net mvc application. I want session to never expire, once the user login, unless the user clicks on logout.

Whats the best way of doing it?

I have used

FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);

and set createPersistentCookie to true, but still I get logged out after sometime.

A: 

I'm looking at that too. I already store the user's username/password (hotmail-like, encrypted) in a cookie. So i was thinking to add a field like 'KeepSignedIn' and if it's set to true i bypass the login and make it look like the user stays signed in.

Jeroen
That's horrible security. Never store the password client side and never bypass a login...
Chris
Well if you know how to do it why don't you enlighten us with an answer instead of a comment.
Jeroen
Kinda already did.
Chris
That's lame. If you're not here to help stop posting these comments all together. Instead of saying what not to do help people in the right direction telling them how you did it or would do it.
Jeroen
Jeroen, my first comment was intended to steer any readers away from your method because it's insecure. It was not a personal dig @ you, nor am I the one that downvoted you (actually upvoted you after the first downvote). If you're going to post here, expect that sometimes your advice may be challenged and be prepared to defend or accept it, rather than getting all upset about it.
Chris
That still doesn't help us get to the solution, right, Chris?
Jeroen
No, Jeroen, that would be why I provided an answer in addition to my comment. Seriously, are you really this sensitive about it?
Chris
Hotmail is NOT storing user password on the client. Neither should you, unless you are doing one-way encryption of it (in which case you can't use it to login the user anyway). You shouldn't silently signin the user either, unless the user explicitly choose so.
Franci Penov
@Franci Penov: Ok i don't know how they do it. But i don't silenty sign in my users, that was just a thought on how to keep them signed in. I will read up on the FormsAuthentication.
Jeroen
+1  A: 

FormsAuthentication cookies use the timeout value to determine expiration. The createPersistentCookie flag just tells the API to set an expires value, rather than allowing the cookie to expire when the browser is closed. To prevent expiration, increase the forms authentication timeout value in the web.config. That value is in minutes, so to force the cookie to last one year, use 525600.

Chris
Well, that will take a lot of memory on the server. Is there any alternative?
San
Huh? What will take a lot of memory? It's a single int value.
Chris
that is not going totake any memory on the server. cookie expiration is done through a imestamp on the client.
Franci Penov
Ok then increasing forms authentication timeout value, should be fare enough. Which one is better sliding expiration or the above one, or it doesnt matter?
San
I'd go with Franci's sliding expiration suggestion. It's slightly more secure and desirable than a 1 year cookie, IMO
Chris
however, leaving the cookie there for a year is not a good idea. you want to be a bit mote aggressive with expiring inactive user sessions, especially if any of the user information might be sensitive.
Franci Penov
Right. 1 week is probably long enough. If the site is heavily used, 2-3 days might even suffice.
Chris
Ok, then I will go with sliding expiration, do you know any good article where its explained, preferably with asp.net mvc?
San
just search for "asp.net forms authentication sliding expiration". there's bound to be tons of articles. :-)
Franci Penov
+3  A: 

Implement sliding expiration. Leave the expiration time to some reasonable value - day, two, week max; renew the cookie on each request (simplest) or at certain intervals.

Franci Penov
This will also work, and is probably better than using an expiry of 1 year
Chris