我在处理cookie /会话超时方面遇到了一些问题,我需要一个持久性cookie才能在会话超时时间到期,但之后。问题是,当.net会话在超时后消失,没有找到cookie值时,这是我用来设置cookie并检索它的代码。
设置cookie的代码:
HttpCookie cookiecontext = new HttpCookie("cookiecontext");
DateTime now = DateTime.Now;
cookiecontext["Context"] = "MB";
cookiecontext.Expires = now.AddHours(5);
HttpContext.Current.Response.Cookies.Add(cookiecontext);
在超时发生时在错误页面获取cookie的代码(VerifyCookie()函数在使用后清除cookie):
if (HttpContext.Current.Request.Cookies["cookiecontext"] != null )
{
if (HttpContext.Current.Request.Cookies["cookiecontext"]["Context"] == "MB")
{
controllerOwn = (IController)AppContextMobile.ApplicationContext.GetObject(controllerName.ToUpper());
VerifyCookie();
}
else
{
controllerOwn = (IController)AppContextWeb.ApplicationContext.GetObject(controllerName.ToUpper());
}
}
你知道为什么在超时后不考虑cookie吗?对我来说,这段代码应该已经完成了,但显然它没有。
提前感谢所有阅读此问题的人。
编辑:添加一些信息,我正在使用Asp.net Mvc 2,代码的想法是识别用户是在移动设备还是网络环境中,向他展示合适的信息超时页面为每个设置样式,因为我的所有变量在超时后死亡,cookie是我唯一的选择,Idk为什么我不能直接使用Request.cookies或response.cookies,它只允许我使用HttpContext.current.Response / Request
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using PBT.Models;
using PBT.Models.Interfaces.Mobile.ViewEngine.Configurator;
using PBT.ViewEngine.Configurator.Modules;
using PBT.ViewEngine.Configurator.Modules.Utilities;
namespace PBT.Controllers.Factory
{
public class QuieroControllerFactory : IControllerFactory
{
#region Members of IControllerFactory
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
try
{
IController controllerOwn;
string Ruta = controllerName + "/" + GetAction(requestContext);
if (Ruta.Equals("Home/WelcomePage"))
{
if (Convert.ToString(requestContext.HttpContext.Request.Form["Canal"]) == "MB")
{
HttpCookie CookieContext = new HttpCookie("CookieContext");
DateTime now = DateTime.Now;
CookieContext["Context"] = "MB"; //User in a mobile environment
CookieContext.Expires = now.AddHours(5);
HttpContext.Current.Response.Cookies.Add(CookieContext);
HttpContext.Current.Session.Add("Context", "MB");
}
if (Convert.ToString(requestContext.HttpContext.Request.Form["Canal"]) == "HB")
{
HttpCookie CookieContext = new HttpCookie("CookieContext");
DateTime now = DateTime.Now;
CookieContext["Context"] = "HB"; //User in a web environment
CookieContext.Expires = now.AddHours(5);
HttpContext.Current.Response.Cookies.Add(CookieContext);
HttpContext.Current.Session.Add("Context", "HB");
}
}
if (HttpContext.Current.Session["context"] != null)
{
if (HttpContext.Current.Session["context"] == "MB")
{
controllerOwn = (IController)AppContextMobile.ApplicationContext.GetObject(controllerName.ToUpper());
}
else
{
controllerOwn = (IController)AppContextWeb.ApplicationContext.GetObject(controllerName.ToUpper());
}
}
else
{
if (HttpContext.Current.Request.Cookies["CookieContext"] != null)
{
if (HttpContext.Current.Request.Cookies["CookieContext"]["Contexto"] == "MB")
{
controllerOwn = (IController)AppContextMobile.ApplicationContext.GetObject(controllerName.ToUpper());
}
else
{
controllerOwn = (IController)AppContextWeb.ApplicationContext.GetObject(controllerName.ToUpper());
}
}
else
{
controllerOwn = (IController)AppContextWeb.ApplicationContext.GetObject(controllerName.ToUpper());
}
}
return controllerOwn;
}
catch (Spring.Objects.Factory.NoSuchObjectDefinitionException ex)
{
return null;
}
catch (Exception ex)
{
throw;
}
}
public void ReleaseController(IController controller)
{
if (controller is IDisposable)
{
(controller as IDisposable).Dispose();
}
controller = null;
}
#endregion
private static string GetAction(System.Web.Routing.RequestContext requestContext)
{
return Convert.ToString(requestContext.RouteData.Values["action"]);
}
public void VerifyCookie()
{
if (HttpContext.Current.Response.Cookies["CookieContext"] != null)
{
HttpCookie myCookie = new HttpCookie("CookieContext");
myCookie.Expires = DateTime.Now.AddDays(-1d);
HttpContext.Current.Response.Cookies.Add(myCookie);
}
}
}
}