如何设置会话过期时间使用ASP.NET MVC5上的代码? [英] How setting session expired time use code on ASP.NET MVC5?

查看:78
本文介绍了如何设置会话过期时间使用ASP.NET MVC5上的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不使用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?

也许只持续30分钟,也许用户选择记住我" ,然后将其保存24小时.

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

第二个问题是:如果我已经在web.config上设置了过期时间,例如:

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?

推荐答案

您可以使用此方法在 session_start 事件上的代码 global.asax 中设置会话.

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

Session.Timeout = 24 * 60;

我已经签入了代码,像上面那样在 code 中设置会话会覆盖在 web.config 中的会话.因此,上述优先级更高,将被优先使用,并且 web.config 的值将被丢弃.

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.

这篇关于如何设置会话过期时间使用ASP.NET MVC5上的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆