CefSharp 删除 Cookie 的方法

.Net技术 码拜 8年前 (2016-02-18) 16522次浏览 0个评论

CefSharp 提供删除Cookie的方法,使用ICookieManager接口中的DeleteCookiesAsync方法
///
/// Deletes all cookies that matches all the provided parameters asynchronously. If both and are empty, all cookies will be deleted.
///
///The cookie URL. If an empty string is provided, any URL will be matched.
///The name of the cookie. If an empty string is provided, any URL will be matched.
/// A task that represents the delete operation. The value of the TResult parameter contains false if a non-empty invalid URL is specified, or if cookies cannot be accessed; otherwise, true.
Task DeleteCookiesAsync(string url, string name);

具体应用:Cef.GetGlobalCookieManager().DeleteCookiesAsync(string url, string name);

以上方法在应用过程中发现有问题,例如:当删除指定url=”http://www.codebye.com”的cookie时,不能完全将cookie 完全删除,只删除了一部分,对下次取cookie造成重复。

经过查看源码发现可以使用替代方案,cef_cookie.h中有描述(红色部分):

///
// Delete all cookies that match the specified parameters. If both |url| and
// |cookie_name| values are specified all host and domain cookies matching
// both will be deleted. If only |url| is specified all host cookies (but not
// domain cookies) irrespective of path will be deleted. If |url| is empty all
// cookies for all hosts and domains will be deleted. If |callback| is
// non-NULL it will be executed asnychronously on the IO thread after the
// cookies have been deleted. Returns false if a non-empty invalid URL is
// specified or if cookies cannot be accessed. Cookies can alternately be
// deleted using the Visit*Cookies() methods.
///
/*–cef(optional_param=url,optional_param=cookie_name,
optional_param=callback)–*/
virtual bool DeleteCookies(const CefString& url,
const CefString& cookie_name,
CefRefPtr<CefDeleteCookiesCallback> callback) =0;

具体实现是使用CefCookieVisitor中的visit方法的第三个参数,方法说明:

///
// Method that will be called once for each cookie. |count| is the 0-based
// index for the current cookie. |total| is the total number of cookies.
// Set |deleteCookie| to true to delete the cookie currently being visited.
// Return false to stop visiting cookies. This method may never be called if
// no cookies are found.
///
/*–cef()–*/
virtual bool Visit(const CefCookie& cookie, int count, int total,
bool& deleteCookie) =0;


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明CefSharp 删除 Cookie 的方法
喜欢 (2)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!