MFC调用CEF实现单页面单COOKIE管理《转》

cookie简单介绍

cookie存储了网站的一些很重要的信息,如用户身份信息、经常使用设置、设置地理位置等等各类信息。
使用cef访问网站时,若是设置了CefSettings.cache_path参数,则cookie等相关信息会保存在该目录下,若未设置则会保存在程序的内存中。chrome

CefSettings settings; CefString(&settings.cache_path) = L"D:\\cefcookie"; 
C++

下图是访问 www.baidu.com 页面保存在指定位置的网站数据,能够看的其中也包括Cookies信息:
浏览器

多页面共享cookie问题

有时某个网站咱们想登陆多个帐号,可是会发现登陆新的帐号后,旧帐号就退出了,即使打开多个浏览器操做结果也是同样的。这是由于新登陆帐号的cookie覆盖了旧帐号的cookie,旧帐号天然就失效了。
google chrome提供了一种“无痕窗口”模式,在菜单栏能够打开无痕窗口,该模式不会保存cookie等网站信息,旨在保护用户隐私。发如今该模式下登陆的帐号和在普通页面上登陆的帐号,能够同时保持在线,其实就是由于cookie等信息存储在不一样的位置,不会互相覆盖。cookie

若设置了CefSetting.cache_path,则全部页面cookie都会保存在该目录下,当登陆某网站多个帐号时,cookie被覆盖,就只能保持一个帐号在线了。若不设置cache_path则cookie会保存在内存中,登陆多个帐号时cookie一样会被覆盖。网站

单页面cookie存储位置设置

解决上述问题,天然想到把每一个页面的cookie单独存储就行了。在建立浏览器时经过CefRequestContextSettings能够指定页面cookie等信息存储位置,以下代码:google

// 建立浏览器 void CefHelper::CreateBrowser(CWnd* parent, CRect rc, CString strUrl, CString strCookiePath, bool bShow) { m_handler = new CSimpleHandler(false); CefWindowInfo winInfo; winInfo.SetAsChild(parent->GetSafeHwnd(), rc); CefRequestContextSettings settings; CefString(&settings.cache_path).FromWString(wstring(strCookiePath)); CefRefPtr<CefRequestContextHandler> handlerCookie = new WXRequestContextHandler(wstring(strCookiePath).c_str()); CefRefPtr<CefRequestContext> rContext = CefRequestContext::CreateContext(settings, handlerCookie); CefBrowserSettings browserSettings; CefBrowserHost::CreateBrowser(winInfo, m_handler, wstring(strUrl).c_str(), browserSettings, rContext); } 
C++
 

文章导航

 http://www.itzhi365.com/?p=253spa