|
<< Click to Display Table of Contents >> Navigation: Forum > Cookie Demo needed |
04-24-2019, 02:42 AM:
Is there any cookie usage demos? There isn't one in the Github download of demos.
Using Tokyo 10.2.1
IW 15.0.20
Stand Alone Server
TIWStandAloneServer being used to have a custom server form.
AllowMultipleSessionsPerUser = TRUE
For our VCL / FMX Mobile App... gotta supply 4 values to login in to the service.
Two of them can be restored back from a cookie when you come back to page at a later date/time... the other two values have to be put in.
I am not 100% sure the proper usages of
WebApplication.Response.Cookies.AddCookie
and
webapplication.Request.CookieFields.Values
of when I should be using these... And for the addCookie, what kind of parameters I should be using for PATH and such as I don't want it
tied to a the random generated session path/id.
Thanks!
04-24-2019, 06:02 AM:
Here is something from the old forum (forum is now readonly)
https://forums.embarcadero.com/thread.js...421#693421
Also see:
https://forums.embarcadero.com/thread.js...882#882882
From Alex:
Reading cookie value:
Class THttpRequest, method GetCookieValue:
function GetCookieValue(const aName: string): string;
Writing a cookie:
Class THttpReply, use method AddCookie from Cookies object (type THttpCookieList), e.g.:
Reply.Cookies.AddCookie()
function AddCookie(const aName, aValue, aPath: string; const aExpires: TDateTime; const aHttpOnly: Boolean = False; const aSecure: Boolean = False): Integer;
Also from Alex in the second thread, HTTPOnly...
Is there a way to set the HTTPOnly flag in the Set-Cookie HTTP
response header?
Is this a custom cookie or the default session tracking cookie?
If this is the default session tracking cookie, just use the
ServerController.CookieOptions property:
- set CookieOptions.HttpOnly to True/False, according to your
requirements
If this is a custom cookie, you can set this option when adding the
cookie to the response cookie collection:
THttpReply.Cookies is the cookie coolection object.
You use its Add() method to add a custom cookie to your response, and
this method has this signature:
THttpCookieList.AddCookie(const aName, aValue, aPath: string; const
aExpires: TDateTime; const aHttpOnly: Boolean = False; const aSecure:
Boolean = False): Integer;
Dan
04-25-2019, 05:05 AM:
I ran into those too.. either calls changed, or missing some information in those original posts.
I need cookies that are not locked to the session. So user can come back at a later date and data is there to grab?
04-25-2019, 06:14 AM:
(04-25-2019, 05:05 AM)cpstevenc Wrote: [ -> ]I ran into those too.. either calls changed, or missing some information in those original posts.
I need cookies that are not locked to the session. So user can come back at a later date and data is there to grab?
Try this to set your cookie.
Code:
webapplication.Response.Cookies.Add(THTTPCookie.Create(CookieName,CookieValue,'/',Expireon,true));
ExpireOn is the UTCDateTime (as a Real). Set to -1 for a session cookie.
Name/Value are String.
Try this to read the cookie:
Code:
Result := webapplication.Request.CookieFields.Values[sName];
It should work, but there may be better ways.
Dan
05-05-2019, 05:57 AM:
(04-25-2019, 06:14 AM)DanBarclay Wrote: [ -> ] (04-25-2019, 05:05 AM)cpstevenc Wrote: [ -> ]I ran into those too.. either calls changed, or missing some information in those original posts.
I need cookies that are not locked to the session. So user can come back at a later date and data is there to grab?
Try this to set your cookie.
Code:
webapplication.Response.Cookies.Add(THTTPCookie.Create(CookieName,CookieValue,'/',Expireon,true));
ExpireOn is the UTCDateTime (as a Real). Set to -1 for a session cookie.
Name/Value are String.
Try this to read the cookie:
Code:
Result := webapplication.Request.CookieFields.Values[sName];
It should work, but there may be better ways.
Dan
Thanks!
UTCDateTime was my issue.
Also used Chrome inspector and took me a bit to find the call that was actually kicking off the COOKIE code to save and saw my date being used was in like 1927 .... so expired