How to SetCookie?SetCookie not working in v109.0.5414.87

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

How to SetCookie?SetCookie not working in v109.0.5414.87

Postby YogeshTembe » Fri Jun 16, 2023 4:26 am

Hi
I have been trying to set a cookie in OnBeforeResource Load
CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager(nullptr);
CefCookie cookie;
CefString(&cookie.name).FromString("test");
CefString(&cookie.value).FromString("value");
CefString(&cookie.domain).FromString(".google.com"); // sample domain: .domain.com
CefString(&cookie.path).FromASCII("/");
cookie.has_expires = false;
CefRefPtr<CefWaitableEvent> event = CefWaitableEvent::CreateWaitableEvent(true, false);
CefRefPtr<CefSetCookie> setCookieCallback = new CefSetCookie(manager);
setCookieCallback->SetEvent(event);
bool result = manager->SetCookie("https://www.google.com", cookie,setCookieCallback);
event->Wait();
I receive the result as true but when I log the request headers the cookie does not seem to be set there and is also not visible in database.I also tried setting the cookie before creation of browser and called the manager->FlushStore() but still the cookie is not visible in the db.If someone could please let me know where I might be going wrong and which is the right place to set the cookie and validate that it is being sent in the request to the browser it will be really helpfull.Thank you!
YogeshTembe
Techie
 
Posts: 23
Joined: Tue Sep 14, 2021 6:01 am

Re: How to SetCookie?SetCookie not working in v109.0.5414.87

Postby magreenblatt » Fri Jun 16, 2023 4:57 am

Don’t block CEF threads. You need to set the cookie before loading the URL that requires it.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How to SetCookie?SetCookie not working in v109.0.5414.87

Postby YogeshTembe » Fri Jun 16, 2023 5:52 am

Thank you for the prompt reply
Initially I was setting the cookie in OnBeforeResourceLoad without waiting for it's completion as shown below
CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager(nullptr);
CefCookie cookie;
CefString(&cookie.name).FromString("test");
CefString(&cookie.value).FromString("value");
CefString(&cookie.domain).FromString(".google.com"); // sample domain: .domain.com
CefString(&cookie.path).FromASCII("/");
cookie.has_expires = false;
CefRefPtr<CefSetCookie> setCookieCallback = new CefSetCookie(manager);
manager->SetCookie("https://www.google.com", cookie,setCookieCallback);

But this also does not seem to set the cookie as the request headers only displays the cookies that are set by the browser and not the ones I'm trying to set
Output:
CookieAEC=AUEFqZdlR0Oq0_tDIZj9Rzi2Flj_G1HxoM78DtdUrkHm7wX2UN3btXOp9w; OGPC=19027681-1:; NID=511=f8v6Bzvgg_DChiFAQWfFM7iHyxsRJIMjKijnwxOSYvOp3D6rv4WFaIUJu4nmcM1wN8mibYXtqkl8xJ7rKym69sX1WefUW38ZW87cyUYOJTgciOE0-b3srM2yxvv0Yy1BKod9xIFRsSv11t6IGiT2z2IxKtbO2uUYw55mcaGDdfo; 1P_JAR=2023-06-16-10
YogeshTembe
Techie
 
Posts: 23
Joined: Tue Sep 14, 2021 6:01 am

Re: How to SetCookie?SetCookie not working in v109.0.5414.87

Postby magreenblatt » Fri Jun 16, 2023 1:23 pm

Where is the cookie data originating from?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How to SetCookie?SetCookie not working in v109.0.5414.87

Postby YogeshTembe » Sun Jun 18, 2023 3:02 pm

Sorry for the delayed response
I’m creating my own cookie with dummy values and trying to set it for a particular url (in this case google.com) however the request header does not display the cookie I’m trying to set which means that the cookie I created is not being sent to the browser with the request.I’m unable to understand where I might be going wrong as there is no way for me to validate the cookie is correct and set.Please help me out!Thank you.
YogeshTembe
Techie
 
Posts: 23
Joined: Tue Sep 14, 2021 6:01 am

Re: How to SetCookie?SetCookie not working in v109.0.5414.87

Postby HarmlessDave » Sun Jun 18, 2023 6:25 pm

Setting in OnBeforeBrowse should work if you use the cookie manager.
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: How to SetCookie?SetCookie not working in v109.0.5414.87

Postby YogeshTembe » Sun Jun 18, 2023 10:58 pm

Thank you for your reply
I have tried setting the cookie in OnBeforeBrowse as well still it doesn't seem to work.Could it be an issue because of the version?
YogeshTembe
Techie
 
Posts: 23
Joined: Tue Sep 14, 2021 6:01 am

Re: How to SetCookie?SetCookie not working in v109.0.5414.87

Postby ndesktop » Mon Jun 19, 2023 2:29 am

On 111 it works for me. Try an cookie manager visit in code before and after calling SetCookie.
ndesktop
Master
 
Posts: 756
Joined: Thu Dec 03, 2015 10:10 am

Re: How to SetCookie?SetCookie not working in v109.0.5414.87

Postby YogeshTembe » Mon Jun 19, 2023 3:10 am

Thankyou for the reply
Before setting the cookie if a call visit I get no cookies but after setting the cookie visit is called by cef and shows me the cookie I have been trying to set however the request header still does not show me the cookie.
auto visitor = new CefCookieVisitor();
manager->VisitAllCookies(visitor);
manager->SetCookie(url, cookie, nullptr);
manager->VisitAllCookies(visitor);
Do I need to check for any user_preference that needs to be enabled for set cookie to work or is there any pre requisite that needs to be followed before setting a cookie??I have been trying to send my cookie in the request for google.com could it be an issue with the cookie policy set for google?
YogeshTembe
Techie
 
Posts: 23
Joined: Tue Sep 14, 2021 6:01 am

Re: How to SetCookie?SetCookie not working in v109.0.5414.87

Postby ndesktop » Mon Jun 19, 2023 9:29 am

Code: Select all
bool
ClientHandler::OnBeforeBrowse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
    CefRefPtr<CefRequest> request, bool user_gesture, bool isRedirect)
{
...
    setPmCookiesIfAvailable(browser, request);
...
}


In the next function pmCookies_ is a std::vector<CefCookie> loaded from an external source.
Code: Select all
void
ClientHandler::setPmCookiesIfAvailable(
    CefRefPtr<CefBrowser> browser,
    CefRefPtr<CefRequest> request)
{
    do {
        if(pmCookies_.empty())
            break;

        if(!request)
            break;
        CefString url = request->GetURL();
        if(url.empty())
            break;
        CefURLParts url_parts;
        if(!CefParseURL(url, url_parts))
            break;
        CefString host(&url_parts.host);
        if(host.empty())
            break;
        std::string strHost = host.ToString();
        if(strHost.empty())
            break;
        std::string strHostKey = strHost;
        if(strHostKey.find(".") != 0) {
            strHostKey = std_utils::StringPrintf(".%s", strHost.c_str());
        }

        // determine the cookies for this URL - match by domain
        std::vector<CefCookie> pmCookiesForUrl;
        for(const auto& e : pmCookies_) {
            do {
                CefString domain(&e.domain);
                if(domain.empty())
                    break;
                std::string strDomain = domain.ToString();
                if(strDomain.empty())
                    break;
                if(strDomain == strHost || strDomain == strHostKey) {
                    //  hit
                    pmCookiesForUrl.push_back(e);
                }
            } while(0);
        }

        if(pmCookiesForUrl.empty())
            break;

        // set all the cookie(s) for the URL
        CefRefPtr<CefRequestContext> request_context = CefRequestContext::GetGlobalContext();
        if(!request_context)
            break;
        CefRefPtr<CefCookieManager> cookie_mgr = request_context->GetCookieManager(nullptr);
        if(!cookie_mgr)
            break;

        for(const auto& cookie : pmCookiesForUrl) {
            cookie_mgr->SetCookie(url, cookie, nullptr);
        }
    } while(0);
}

That is working for me.
ndesktop
Master
 
Posts: 756
Joined: Thu Dec 03, 2015 10:10 am

Next

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 217 guests