如何在MVC 5上设置会话变量过期时间? [英] How set a Session variable expired time on MVC 5?
问题描述
我需要生成一个验证码,例如: 1599 进行登录.
I need to Generate a Verification Code like: 1599 for Login.
登录时用户必须输入正确的号码.
The User must input correct number when Login in.
但是我的默认会话过期时间为 30分钟.
But My default session expired time is 30 minute.
我不想让验证码过期这么长时间.我只需要保持 1分钟. 那么,如何仅为验证码会话变量设置过期超时?
I don't want let the Verification Code expired time so long. I just need it keep only 1 minute. So How I set expired time out only for the Verification Code session variable?
更新1:
我不想更改全局会话的过期时间.我只需要 在登录前仅对验证码会话变量进行设置.
I don't want to change the global session expired time. I just need set only for the Verification Code session variable before login.
推荐答案
对不起,我终于使用了此解决方案:
Sorry guys, I finally use this solution:
生成验证码时,我会将其保存到会话中
when generate the Verification Code I will save it to session
Session["VerificationCode"] = code;
每个人都知道.但是时间呢?我更改它:
That's everybody knows. but how about the time? I change it :
Session["VerificationCode"] = code;
Session["VerificationTime"] = DateTime.Now;
现在也节省会话时间.然后我可以在用户登录时找到它:
Now the time save in session too. Then I can find it when User Login:
// Verification Code Expired
if ((DateTime)Session["VerificationTime"]).AddMinutes(1) < DateTime.Now)
{
ModelState.AddModelError("VerificationCode", "Verification Code Expired!");
return View(adminLogin);
}
// Verification Code Error
if (Session["VerificationCode"].ToString().ToUpper() != adminLogin.VerificationCode.ToUpper())
{
ModelState.AddModelError("VerificationCode", "Verification Code Error");
return View(adminLogin);
}
这篇关于如何在MVC 5上设置会话变量过期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!