SetCookie returns true but not actually setting cookie

Having problems with building or using the CefGlue .NET/Mono binding? Ask your questions here.

Moderator: fddima

SetCookie returns true but not actually setting cookie

Postby beeplusplus » Fri Oct 18, 2013 6:21 am

Hi,

I am experiencing a problem with SetCookie. SetCookie() returns true but actually not setting the cookie. Please, help me to pindown the issue. I suspect something wrong the characters in the "Value" string.

Cookie Name:-
TFSessionId

Cookie Value:-
Z1NBCMSse/7v/V6g6aBNGeeYDkbMoitRZdeV4V19kAvfjVhpBya8kBdsM1cE/I9RHDw4DbBYLaQBrE9mhGiJ9JRRM0eDJK3zTjPPr/iZWVd5jZJTHFFxe5AoYocYpdi5Gxk+uEEbXCDrMFSK5mDxV/
QSuN6LanhDkhfNYjuuZUORxLzEQu+/o0ki/K5X1P7sEgLGJyvByKZ9c2rSiREZBg==;domain=.atozqa.com

Here's the code snippet:-

public class ExtendedTaskSetCookie : CefTask
{
private string url;
private CefCookie cookie;
public bool Success { get; set; }

public ExtendedTaskSetCookie(string url, CefCookie cookie)
{
Success = false;
this.url = url;
this.cookie = new CefCookie();
this.cookie.Name = (string) cookie.Name.Clone();
this.cookie.Value = (string) cookie.Value.Clone();
}

protected override void Execute()
{
Success = CefCookieManager.Global.SetCookie(url, cookie);
Debug.WriteLine(Success);
}
}

Thanks
~Bala
Last edited by beeplusplus on Fri Oct 18, 2013 8:14 am, edited 2 times in total.
beeplusplus
Techie
 
Posts: 17
Joined: Fri Aug 30, 2013 6:48 am

Re: SetCookie returns true but not actually setting cookie

Postby fddima » Fri Oct 18, 2013 6:41 am

Hi.

Try set Domain in cookie, i'm not sure that empty domain is really allowed.
Or explain little detailed how it can be easily reproduced (checked).
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: SetCookie returns true but not actually setting cookie

Postby beeplusplus » Fri Oct 18, 2013 8:01 am

Hi,

Thanks for the reply. Seems have resolved the issue by Encoding the "Value" part of cookie by using HttpUtility.UrlEncode() method.

The sad part is... SetCookie() returns true though it is not really successful. So, I had spent a week time, suspecting thread, cloning/reference, url, domain, ceflibrary, etc... Oops! A bad API -- SetCookie().

public ExtendedTaskSetCookie(string url, CefCookie cookie)
{
Success = false;
this.url = url;
this.cookie = cookie;
//Encode the "Value" as it contains special / disallowed characters, otherwise cookie will not set be successfully.
//Note:- Though/When "Value" is not not Encoded, SetCookie() API returns success (true) but actually not.
this.cookie.Value = HttpUtility.UrlEncode(cookie.Value);
}

protected override void Execute()
{
Success = CefCookieManager.Global.SetCookie(url, cookie);
Debug.WriteLine(Success);
}
beeplusplus
Techie
 
Posts: 17
Joined: Fri Aug 30, 2013 6:48 am

Re: SetCookie returns true but not actually setting cookie

Postby beeplusplus » Fri Oct 18, 2013 8:08 am

hi Fddima,

Actually, cookie.Value contains the domain; that's the culprit, because it was separated with semicolon ';', which needed to be encoded:-

Value Before Encode:-
"Z1NBCMSse/7v/V6g6aBNGeeYDkbMoitRZdeV4V19kAtgkbIFNDA7TOk2/Nln3mjNaTvoOGPfm7fMA1wCCOj8zWLy5/
zoHFGhYnA483zr4llrw+cmoLm66rfOMlxxMaxEsE3NKg0Db44JpLjiV4uJ/u6Be97ILpQ6U7JO6vSnBv0PJfgFi+qGK5XIZOtS1GBjEIDvLgh2SxMWUw/
nSJ6myA==;domain=.atozqa.com"


Value After Encode:-
"%22Z1NBCMSse%2F7v%2FV6g6aBNGeeYDkbMoitRZdeV4V19kAtgkbIFNDA7TOk2%2FNln3mjNaTvoOGPfm7fMA1wCCOj8zWLy5%2F
zoHFGhYnA483zr4llrw%2BcmoLm66rfOMlxxMaxEsE3NKg0Db44JpLjiV4uJ%2Fu6Be97ILpQ6U7JO6vSnBv0PJfgFi%2BqGK5XIZOtS1GBjEIDvLgh2SxMWUw%2F
nSJ6myA%3D%3D%3Bdomain%3D.atozqa.com%22"
beeplusplus
Techie
 
Posts: 17
Joined: Fri Aug 30, 2013 6:48 am

Re: SetCookie returns true but not actually setting cookie

Postby fddima » Fri Oct 18, 2013 8:31 am

CefCookie is not raw cookie value! I think you need fill all fields correctly.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: SetCookie returns true but not actually setting cookie

Postby beeplusplus » Fri Oct 18, 2013 8:44 am

Hi,

Yeah, Name and Value are vital. I am passing setting those value before before creating instance of ExtendedTaskSetCookie().
public static class ChromeHelper
{
public static void SetCookie(string urlString, string name, string value)
{
CefCookie cefCookie = new CefCookie();
cefCookie.Name = name;
cefCookie.Value = value;

ExtendedTaskSetCookie extendedTaskSetCookie = new ExtendedTaskSetCookie(urlString, cefCookie);
bool success = CefRuntime.PostTask(CefThreadId.IO, extendedTaskSetCookie);

Debug.WriteLine(success); // it will be always true, as the set cookie is done asynchronously in ExtendedTaskSetCookie.Execute().
Debug.WriteLine(extendedTaskSetCookie.Success); // it will be always false, as the set cookie is done asynchronously in ExtendedTaskSetCookie.Execute().
}
}
beeplusplus
Techie
 
Posts: 17
Joined: Fri Aug 30, 2013 6:48 am

Re: SetCookie returns true but not actually setting cookie

Postby fddima » Fri Oct 18, 2013 9:31 am

I'm not understand you.
CefCookie members contains many properties, while you are set only two of them, and they no have chance work, as you expect. Try set all cookie properties.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: SetCookie returns true but not actually setting cookie

Postby beeplusplus » Fri Oct 18, 2013 9:44 am

Setting these two -- Name and Value just works for me.

My requirement is just session cookie (i.e., the cookies lifetime is, till I close my Application).
beeplusplus
Techie
 
Posts: 17
Joined: Fri Aug 30, 2013 6:48 am

Re: SetCookie returns true but not actually setting cookie

Postby fddima » Fri Oct 18, 2013 10:09 am

beeplusplus wrote:Setting these two -- Name and Value just works for me.

My requirement is just session cookie (i.e., the cookies lifetime is, till I close my Application).

Cookie lifetime controlled by Expired property. Set it to some minimum value, probably to unix epoch, if DateTime.MinValue won't work.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: SetCookie returns true but not actually setting cookie

Postby magreenblatt » Fri Oct 18, 2013 10:41 am

Session cookies are deleted on application exit by default unless you set CefSettings.persist_session_cookies or call CefCookieManager::CreateManager with a true |persist_session_cookies| value. Creating a cookie without a domain component is probably undefined behavior.
magreenblatt
Site Admin
 
Posts: 12383
Joined: Fri May 29, 2009 6:57 pm

Next

Return to CefGlue Forum

Who is online

Users browsing this forum: No registered users and 18 guests