Minimal example for cefsharp and cookies in c#

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Minimal example for cefsharp and cookies in c#

Postby Sven » Sat Jul 09, 2016 2:37 pm

Hi,

i'm looking for a simple example to read/write the value of cookies in a embedded cefsharp application in c#-forms.

I started with this simple example:
https://github.com/cefsharp/CefSharp.MinimalExample (see code-snippet below)

or with this:
https://github.com/cefsharp/CefSharp/tr ... r/CefSharp

and then I implemented the interfaces ICookieManager and ICookieVisitor.

What is the next step to get access to the cookies?

Thank you for your answer.

Code: Select all
namespace CefSharp.MinimalExample.WinForms
{
    public partial class BrowserForm : Form, CefSharp.ICookieManager, CefSharp.ICookieVisitor
    {
        private readonly ChromiumWebBrowser browser;

        public BrowserForm()
        {
            InitializeComponent();

            Text = "CefSharp";
            WindowState = FormWindowState.Maximized;

            browser = new ChromiumWebBrowser("www.google.com")
            {
                Dock = DockStyle.Fill,
            };
            toolStripContainer.ContentPanel.Controls.Add(browser);

            browser.LoadingStateChanged += OnLoadingStateChanged;
            browser.ConsoleMessage += OnBrowserConsoleMessage;
            browser.StatusMessage += OnBrowserStatusMessage;
            browser.TitleChanged += OnBrowserTitleChanged;
            browser.AddressChanged += OnBrowserAddressChanged;                                                                     

            var bitness = Environment.Is64BitProcess ? "x64" : "x86";
            var version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}, Environment: {3}", Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion, bitness);
            DisplayOutput(version);
        }

        private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
        {
            DisplayOutput(string.Format("Line: {0}, Source: {1}, Message: {2}", args.Line, args.Source, args.Message));
        }

        private void OnBrowserStatusMessage(object sender, StatusMessageEventArgs args)
        {
            this.InvokeOnUiThreadIfRequired(() => statusLabel.Text = args.Value);
        }

        private void OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs args)
        {
            SetCanGoBack(args.CanGoBack);
            SetCanGoForward(args.CanGoForward);

            this.InvokeOnUiThreadIfRequired(() => SetIsLoading(!args.CanReload));
        }

        private void OnBrowserTitleChanged(object sender, TitleChangedEventArgs args)
        {
            this.InvokeOnUiThreadIfRequired(() => Text = args.Title);
        }

        private void OnBrowserAddressChanged(object sender, AddressChangedEventArgs args)
        {
            this.InvokeOnUiThreadIfRequired(() => urlTextBox.Text = args.Address);
        }

        private void SetCanGoBack(bool canGoBack)
        {
            this.InvokeOnUiThreadIfRequired(() => backButton.Enabled = canGoBack);
        }

        private void SetCanGoForward(bool canGoForward)
        {
            this.InvokeOnUiThreadIfRequired(() => forwardButton.Enabled = canGoForward);
        }

        private void SetIsLoading(bool isLoading)
        {
            goButton.Text = isLoading ?
                "Stop" :
                "Go";
            goButton.Image = isLoading ?
                Properties.Resources.nav_plain_red :
                Properties.Resources.nav_plain_green;

            HandleToolStripLayout();
        }

        public void DisplayOutput(string output)
        {
            this.InvokeOnUiThreadIfRequired(() => outputLabel.Text = output);
        }

        private void HandleToolStripLayout(object sender, LayoutEventArgs e)
        {
            HandleToolStripLayout();
        }

        private void HandleToolStripLayout()
        {
            var width = toolStrip1.Width;
            foreach (ToolStripItem item in toolStrip1.Items)
            {
                if (item != urlTextBox)
                {
                    width -= item.Width - item.Margin.Horizontal;
                }
            }
            urlTextBox.Width = Math.Max(0, width - urlTextBox.Margin.Horizontal - 18);
        }

        private void ExitMenuItemClick(object sender, EventArgs e)
        {
            browser.Dispose();
            Cef.Shutdown();
            Close();
        }

        private void GoButtonClick(object sender, EventArgs e)
        {
            LoadUrl(urlTextBox.Text);
        }

        private void BackButtonClick(object sender, EventArgs e)
        {
            browser.Back();
        }

        private void ForwardButtonClick(object sender, EventArgs e)
        {
            browser.Forward();
        }

        private void UrlTextBoxKeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
            {
                return;
            }

            LoadUrl(urlTextBox.Text);
        }

        private void LoadUrl(string url)
        {
            if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
            {
                browser.Load(url);
            }
        }

        public Task<bool> DeleteCookiesAsync(string url, string name)
        {
            throw new NotImplementedException();
        }

        public Task<bool> SetCookieAsync(string url, Cookie cookie)
        {
            throw new NotImplementedException();
        }

        public bool SetStoragePath(string path, bool persistSessionCookies)
        {
            throw new NotImplementedException();
        }

        public void SetSupportedSchemes(params string[] schemes)
        {
            throw new NotImplementedException();
        }

        public bool VisitAllCookies(ICookieVisitor visitor)
        {
            throw new NotImplementedException();
        }

        public bool VisitUrlCookies(string url, bool includeHttpOnly, ICookieVisitor visitor)
        {
            throw new NotImplementedException();
        }

        public Task<bool> FlushStoreAsync()
        {
            throw new NotImplementedException();
        }

        public bool Visit(Cookie cookie, int count, int total, ref bool deleteCookie)
        {
            throw new NotImplementedException();
        }
    }
}
Sven
Newbie
 
Posts: 1
Joined: Sat Jul 09, 2016 1:29 pm

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 98 guests

cron