我正在使用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