c# - .net Cookie dissapears after session Timeout -


i have bit of problem handling cookies/session timeout, need persistent cookie expire not @ session timeout, after. problem is, when .net session dissapears after timeout, cookie value not being found, here's code use set cookie , retrieve it.

code sets cookie:

httpcookie cookiecontext = new httpcookie("cookiecontext");                         datetime = datetime.now;                         cookiecontext["context"] = "mb";                         cookiecontext.expires = now.addhours(5);                         httpcontext.current.response.cookies.add(cookiecontext); 

code gets cookie @ errorout page when timeout happens (verifycookie() function clears cookie after using it):

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());                         }                     } 

do have idea why cookie not considered after timeout? me code should have done trick, apparently not.

thanks in advance reads question.

edit: adding info, im working asp.net mvc 2, idea of code recognize wether if user in mobile or web environment, show him appropiate timeout page styled each of them, variables died after timeout, cookies option left, idk why cant use request.cookies or response.cookies directly, allows me use 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 = datetime.now;                         cookiecontext["context"] = "mb"; //user in 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 = datetime.now;                         cookiecontext["context"] = "hb"; //user in 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 idisposable)             {                 (controller 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);             }         }      } } 


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -