A
A
andoral2018-08-04 09:49:58
C++ / C#
andoral, 2018-08-04 09:49:58

Why is recovering cookies (from Json) in CefSharp not working as it should?

Saving cookies
void SaveCookies()
        {
            var cv = new CookieVisitor();
            Cef.GetGlobalCookieManager().VisitAllCookies(cv);
            var json = JsonConvert.SerializeObject(cv.Cookies);
            File.WriteAllText(settingsPath, json);
        }
Restoring cookies
void SetCookies()
        {
            var json = File.ReadAllText(settingsPath);
            var cookies = JsonConvert.DeserializeObject<List<Cookie>>(json);

            if (cookies == null || cookies.Count == 0)
                return;

            foreach(var cookie in cookies)
            {
                Cef.GetGlobalCookieManager().SetCookie(cookie.Domain, cookie);
            }
        }
CookieVisitor
class CookieVisitor : ICookieVisitor
    {

        public List<Cookie> Cookies { get; set; }

        public CookieVisitor()
        {
            Cookies = new List<Cookie>();
        }

        public void Dispose()
        {
            
        }

        public bool Visit(Cookie cookie, int count, int total, ref bool deleteCookie)
        {
            if (cookie == null)
                return false;

            Cookies.Add(cookie);
            return true;
        }
    }

When a cookie is restored, the browser does not restore the session.
For
Cef.GetGlobalCookieManager().VisitAllCookies(cv);
some reason, the input goes directly to cv.Dispose() on the line.
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2018-08-04
@andoral

You need to set CachePath to save all local data instead of storing cookies separately. If CachePath is not set, closing the browser deletes all local data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question