private void DemoWindow_FrameLoadStart(object sender, FrameLoadStartEventArgs e)
{
var cookieManager = CefCookieManager.GetGlobal(null);
var names = new string[] { "company", "founder", "hq"};
var values = new string[] { "Meta", "Mark Zuckerberg", "California"};
for (int i = 0; i < names.Length; i++)
{
var cookie = new CefCookie
{
Name = names[i],
Value = values[i],
Expires = DateTime.Now.AddDays(1)
};
cookieManager.SetCookie(@"http://app/index.html", cookie, new SetCookieCompletion());
}
}
Set/get cookies (using Controller class):
[ChromelyRoute(Path = "/cookiecontroller/set")]
public void SetCookie()
{
var cookieManager = CefCookieManager.GetGlobal(null);
var names = new string[] { "country", "state", "county", "city", "zipcode" };
var values = new string[] { "USA", "New Jersey", "Bergen County", "Saddle Brook", "07663" };
for (int i = 0; i < names.Length; i++)
{
var cookie = new CefCookie
{
Name = names[i],
Value = values[i],
Expires = DateTime.Now.AddDays(1)
};
cookieManager.SetCookie(@"http://app/index.html", cookie, new SetCookieCompletion());
}
}
[ChromelyRoute(Path = "/cookiecontroller/get")]
public void GetCookies()
{
var taskCompletion = new TaskCompletionSource<bool>();
var cookieManager = CefCookieManager.GetGlobal(null);
var cookieVisitor = new CookieVisitor(taskCompletion);
cookieManager.VisitAllCookies(cookieVisitor);
taskCompletion.Task.ContinueWith(task =>
{
var cookies = cookieVisitor.Cookies;
try
{
var scriptExecutor = _config?.JavaScriptExecutor;
if (scriptExecutor != null)
{
if (cookies != null && cookies.Any())
{
StringBuilder builder = new();
foreach (var cookie in cookies)
{
builder.Append($"Name: {cookie.Name} Value: {cookie.Value} <br>");
}
var cookiesStrScript = $"getCookiesResponse('{builder}')";
scriptExecutor.ExecuteScript(cookiesStrScript);
}
else
{
scriptExecutor.ExecuteScript("getCookiesResponse('No cookies found or error retrieving cookies');");
}
}
}
catch (Exception e)
{
Logger.Instance.Log.LogError(e);
}
});
}
Note:
// Note: Normal local scheme - local, does not work with setting cookies, please try http instead
// So this -
// config.StartUrl = "local://app/index.html";
// may not work
config.UrlSchemes.Add(new UrlScheme("custom-local", "http", string.Empty, string.Empty, UrlSchemeType.LocalResource));
config.StartUrl = "http://app/index.html";
Chromely.CrossPlat.zip
Set cookies (using Window event):
Set/get cookies (using Controller class):
Note: