qakmak
qakmak

Reputation: 1377

How setting session expired time use code on ASP.NET MVC5?

I'm not use FormsAuthenticatio.

When the user Login, I will save the user info to on session like:

HttpContext.Current.Session["UserId"] = model.Id;
HttpContext.Current.Session["Name"] = model.Name

But How I set the session expired time use code?

Maybe only for 30 minute, Maybe User select "Remember Me" then save it 24 hour.

Second question is : If I'm already set it expired time on web.config like:

<sessionState mode="InProc" timeout="30" />

Then How about the priority ? config first or code first?

Upvotes: 0

Views: 8198

Answers (2)

Arindam Nayak
Arindam Nayak

Reputation: 7462

You can set session in code, global.asax on session_start event using this.

Session.Timeout = 24*60 ;

I have checked in code that, setting session in code like above override that in web.config. So the above has more priority and this will be used and web.config's value will be discarded.

Upvotes: 3

DividedByZero
DividedByZero

Reputation: 4391

if the user ticks "Remember Me", just add a random guid to a database of active users and give them a cookie with the same guid. when the user comes back without a valid session, check for the cookie and search the database for the guid. if valid, the user is signed in

Upvotes: 0

Related Questions