void SaveCookies()
{
var cv = new CookieVisitor();
Cef.GetGlobalCookieManager().VisitAllCookies(cv);
var json = JsonConvert.SerializeObject(cv.Cookies);
File.WriteAllText(settingsPath, json);
}
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);
}
}
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;
}
}
Cef.GetGlobalCookieManager().VisitAllCookies(cv);