简体   繁体   English

无法设置Cookie:Cef.SetCookie()始终返回false

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

This issue has been puzzling me for a couple of days now. 这个问题困扰了我几天。 I'm completely unable to set a cookie using CefSharp . 我完全无法使用CefSharp设置cookie。

Here's the code block that I'm expecting to work (more so because it's as naive as can be, excluding the explicit thread context switch): 这是我期望工作的代码块(更多原因是因为它天真,不包括显式线程上下文切换):

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...
}));

I'm just wondering if I'm missing some important concept here. 我只是想知道我是否在这里错过了一些重要的概念。 I'm new to CefSharp and despite having pored over the examples and the forums, it's highly possible that I've missed something here. 我是CefSharp ,尽管CefSharp示例和论坛,但很可能在这里错过了一些东西。 Would greatly appreciate any insights or pointers! 将不胜感激任何见解或指针!

Argggh! 啊! I figured it out after more trial and errors. 经过更多的尝试和错误后,我发现了这一点。 A post on this site helped me on my investigation: 该站点上的帖子帮助我进行了调查:

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

Try passing in an empty string for domain rather than "/" or pass in Url as "/mywebsite" and domain as "192.16.1.6" 尝试为域而不是“ /”传递一个空字符串,或者将Url传递为“ / mywebsite”,将域传递为“ 192.16.1.6”

This made me wonder if my cookie params were being rejected for one reason or another. 这使我想知道我的cookie参数是否由于某种原因而被拒绝。 I ended up trying to set the cookie with these params: 我最终尝试使用以下参数设置Cookie:

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

Specifying the URL a bit more rigorously was what did the trick. 窍门是更严格地指定URL。 I guess DNS aliases are not good enough, sometimes. 我猜有时候DNS别名还不够好。 Anyways, I'm going to leave this post in case other CefSharp ers run into a similar situation. 无论如何,如果其他CefSharp遇到类似情况,我将离开该职位。

Here is the code I have used below code to add cookie. 这是我在下面的代码中用于添加cookie的代码。

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

Thank you for suggestion of Akash Patel. 感谢您对Akash Patel的建议。 But that example doesnt work in my case(CefSharp.OffScreen 71.0.2), so I have edited it like this: 但是该示例在我的情况下不起作用(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