c# - Appending multiple values to a Cookie -
i trying store few values in 1 cookie rather creating lot of cookies. have functions named value , set values given names.
when form reloaded cookie seems empty...and on @ least 1 occasion there seemed 2 cookies same name in collection.
the code below, can see have done wrong?
to clarify there 1 cookie, should store several values called code similar below
cookievalues.set("test", "thetestvalue", resp); cookievalues.set("name", "nick", resp); cookievalues.set("sex", "male", resp); var x = cookievalues.get("test", resp); public static class cookievalues { public static void set(string key, string value, httpresponsebase resp) { httpcookie cookie = getcookie(resp); cookie[key] = value; cookie.expires = datetime.now.addyears(1); resp.cookies.add(cookie); } public static string get(string key, httpresponsebase resp) { httpcookie cookie = getcookie(resp); if (cookie[key] != null) { return cookie[key]; } return ""; } private static httpcookie getcookie(httpresponsebase resp) { httpcookie cookie = resp.cookies["holidayadmin"]; if (cookie == null) { cookie = new httpcookie("holidayadmin"); } return cookie; } }
i'm not licensed guru on cookies in c# fear might missing something, interpretation of issue anyway.
you're adding , getting cookies response. should available right after creation (within same response). once response gone, , cookie set on client, you'd have using request (or believe).
one way then, change get
method to:
public static string get(string key, httprequestbase resp)
and, obviously, send request way. should mean can request cookie after has been set on client - i.e. on new request. seems response copies request right on creation, should available within current response.
if it's not though, might idea check both response , request in get
.
Comments
Post a Comment