簡體   English   中英

無法設置Cookie:Cef.SetCookie()始終返回false

[英]Unable to set cookie: Cef.SetCookie() always returns false

這個問題困擾了我幾天。 我完全無法使用CefSharp設置cookie。

這是我期望工作的代碼塊(更多原因是因為它天真,不包括顯式線程上下文切換):

Application.Current.Dispatcher.Invoke(new Action(() =>
{
    var settings = new CefSettings();
    settings.CachePath = "cookies";

    Cef.Initialize(settings);

    Cef.DeleteCookies("", ""); 
    Cef.VisitAllCookies(new CookieVisitor());  // <-- doesn't get called, so assuming we've cleared all the persistent cookies here...

    Cef.SetCookiePath("/", false);
    Cef.VisitAllCookies(new CookieVisitor());  // <-- ok guess im paranoid...
    var isSet = Cef.SetCookie("/", "username", 
                    "testuser", "tovalrsv01", "/", 
                    false, false, false, new DateTime(2020, 1, 1));

    Cef.VisitAllCookies(new CookieVisitor()); // <-- isSet is false, and i don't see the cookie that i created in the visited list...
}));

我只是想知道我是否在這里錯過了一些重要的概念。 我是CefSharp ,盡管CefSharp示例和論壇,但很可能在這里錯過了一些東西。 將不勝感激任何見解或指針!

啊! 經過更多的嘗試和錯誤后,我發現了這一點。 該站點上的帖子幫助我進行了調查:

https://groups.google.com/forum/#!topic/cefsharp/SflbtatvTqQ

嘗試為域而不是“ /”傳遞一個空字符串,或者將Url傳遞為“ / mywebsite”,將域傳遞為“ 192.16.1.6”

這使我想知道我的cookie參數是否由於某種原因而被拒絕。 我最終嘗試使用以下參數設置Cookie:

var isSet = Cef.SetCookie( " http://tovalrsv01:8142/ " , "username", "testuser", "", "/", false, false, false, new DateTime(2020, 1, 1));

竅門是更嚴格地指定URL。 我猜有時候DNS別名還不夠好。 無論如何,如果其他CefSharp遇到類似情況,我將離開該職位。

這是我在下面的代碼中用於添加cookie的代碼。

var mngr = Cef.GetGlobalCookieManager();
Cookie Ac = new Cookie();
Ac.Name = "<Cookie Name>";
Ac.Value = "<Value>";
mngr.SetCookieAsync(<URL to Navigate>, Ac);

感謝您對Akash Patel的建議。 但是該示例在我的情況下不起作用(CefSharp.OffScreen 71.0.2),因此我已對其進行了如下編輯:

//_browser is object of ChromiumWebBrowser
var cookieManager=_browser.RequestContext.GetDefaultCookieManager(null);
Cookie cookie = new Cookie
{
    Name = name,
    Value = value
};
cookieManager.SetCookie(url, cookie);
//or cookieManager.SetCookieAsync(url, cookie);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM