Kom ihåg mig?
Home Menu

Menu


uppdatera expire tiden på befintlig cookie i .net c#?

Ämnesverktyg Visningsalternativ
Oläst 2015-01-14, 15:28 #1
naak2803 naak2803 är inte uppkopplad
Mycket flitig postare
 
Reg.datum: Jun 2011
Inlägg: 767
naak2803 naak2803 är inte uppkopplad
Mycket flitig postare
 
Reg.datum: Jun 2011
Inlägg: 767
Question uppdatera expire tiden på befintlig cookie i .net c#?

Hej,

Har lite problem med att uppdatera tiden på befintlig cookie.

Flöde:
Kod:
//Sätter cookie första gången
HttpCookie myCookie = new HttpCookie("CUser");
myCookie["userId"] = sHashedUserId;
myCookie.Expires = DateTime.Now.AddDays(1d);
HttpContext.Current.Response.Cookies.Add(myCookie);
Kod:
//Uppdatera befintliga cookien
if (cookietiden <= HttpContext.Current.Request.Cookies["CTPUser"].expire)
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies["CTPUser"];
myCookie.Expires = DateTime.Now.AddDays(1d);
HttpContext.Current.Response.Cookies.Add(myCookie);
}
denna kod verkar inte fungera.... dvs den sätter inte om befintliga cookien.

tacksam för svar.
naak2803 är inte uppkopplad   Svara med citatSvara med citat
Oläst 2015-01-14, 15:58 #2
allstars allstars är inte uppkopplad
Klarade millennium-buggen
 
Reg.datum: Apr 2006
Inlägg: 2 126
allstars allstars är inte uppkopplad
Klarade millennium-buggen
 
Reg.datum: Apr 2006
Inlägg: 2 126
Citat:
You cannot directly modify a cookie. Instead, changing a cookie consists of creating a new cookie with new values and then sending the cookie to the browser to overwrite the old version on the client. The following code example shows how you can change the value of a cookie that stores a count of the user's visits to the site:
Kod:
int counter;
if (Request.Cookies["counter"] == null)
    counter = 0;
else
{
    counter = int.Parse(Request.Cookies["counter"].Value);
}
counter++;

Response.Cookies["counter"].Value = counter.ToString();
Response.Cookies["counter"].Expires = DateTime.Now.AddDays(1);
http://technet.microsoft.com/en-us/l...94(VS.80).aspx
allstars är inte uppkopplad   Svara med citatSvara med citat
Svara


Aktiva användare som för närvarande tittar på det här ämnet: 1 (0 medlemmar och 1 gäster)
 

Regler för att posta
Du får inte posta nya ämnen
Du får inte posta svar
Du får inte posta bifogade filer
Du får inte redigera dina inlägg

BB-kod är
Smilies är
[IMG]-kod är
HTML-kod är av

Forumhopp


Alla tider är GMT +2. Klockan är nu 12:40.

Programvara från: vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Svensk översättning av: Anders Pettersson
 
Copyright © 2017