我正在使用mvc.net中的自定义登录页面。我检查登录像这样:.net表单身份验证问题
public bool Login(string login, string password, bool persistent)
{
var loginEntity = this.AdminRepository.GetLogin(login, password);
if (loginEntity != null)
{
FormsAuthentication.SetAuthCookie(login, persistent);
HttpContext.Current.Session["AdminId"] = loginEntity.AdminId;
HttpContext.Current.Session["AdminUsername"] = loginEntity.Username;
return true;
}
然后我装点需要与过滤器属性的管理权限的任何控制器:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var ctx = HttpContext.Current;
// check if session is supported
if (ctx.Session != null)
{
var redirectTargetDictionary = new RouteValueDictionary();
// check if a new session id was generated
if (ctx.Session.IsNewSession)
{
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers["Cookie"];
if (((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) || null == sessionCookie)
{
redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("area", "Admin");
redirectTargetDictionary.Add("action", "LogOn");
redirectTargetDictionary.Add("controller", "Home");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
} else if (SessionContext.AdminId == null) {
redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("area", "Admin");
redirectTargetDictionary.Add("action", "LogOn");
redirectTargetDictionary.Add("controller", "Home");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
}
base.OnActionExecuting(filterContext);
}
我看到日志后我有两个饼干:
- ASPXAUTH(过期日期将 设置为“会话结束时”( 持续存在时为false)或(现在为 (当persist设置为true时)为30分钟
- 和ASP.NET_SessionId这 到期时间始终是“在 会议结束”
问: 的问题是,即使我设置为TRUE,以“坚持”选项(这将设置ASPXAUTH到期时间从现在开始30分钟 - 这很好)我关闭并重新打开浏览器后,Session [“AdminId”]始终为空。当我最初将“持续”设置为true并关闭然后重新打开浏览窗口时,如何确保我的会话(会话[“AdminId”]和会话[“AdminUsername”])从Cookie中被拉入。 谢谢
持久性的工作参数设置,如果设置为true,过期的cookie属性。你在你的web.config中设置了Expires到了什么。你有没有试过用ie来检查cookie的内容。提琴手,看看过期是否设置? – 2011-01-13 21:52:30