MicrosoftEdge / MicrosoftEdge/WebView2Feedback

Cookies are not saved when Webview2 is displayed using .show() method

Open
#2,128 5 comments 1 reaction 1 assignee View on GitHub

@champnic is already working on this.

Since Feb 3, 2022.

bug
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

My webview2 Control resides in a form and i am trying to show it from another form using ShowDialog() and Show().
The url I am displaying needs authentication and I want to skip the authentication when I load it second time. It wont ask for authentication during the second time, if I show the from using .ShowDialog() function. However if I use .Show() function , it is always ask for authentication. Cookies are not saved when it was displayed using .show() method. Can any one help me. Sample code is attached.

    public partial class StartForm : Form
    {
        public StartForm()
        {
            InitializeComponent();
        }

        private void LaunchModalWindowbutton_Click(object sender, EventArgs e)
        {
            WebForm webForm = new WebForm();
            webForm.ShowDialog();
        }

        private void LaunchNonModalWindowButton_Click(object sender, EventArgs e)
        {
            WebForm webForm = new WebForm();
            webForm.Show();
        }
    }


 public partial class WebForm : Form
    {
        private string m_url = "http://ottdev-cs.otxlab.net/CS/llisapi.dll/app/";
        public WebForm()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }

        private async void webView_NavigationCompletedAsync(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
            var cookies = await webView.CoreWebView2.CookieManager.GetCookiesAsync(null);
            for (int i = 0; i < cookies.Count; i++)
            {
                Console.WriteLine("Cookie Name = {0} Value = {1}", cookies[i].Name, cookies[i].Value);
            }
        }


        private async void WebForm_LoadAsync(object sender, EventArgs e)
        {
            await webView.EnsureCoreWebView2Async();
            if (!string.IsNullOrWhiteSpace(m_url) && webView != null && webView.CoreWebView2 != null)
            {
                var cookies = await webView.CoreWebView2.CookieManager.GetCookiesAsync(null);
                for (int i = 0; i < cookies.Count; i++)
                {
                    Console.WriteLine("Cookie Name = {0} Value = {1}", cookies[i].Name, cookies[i].Value);
                }
                webView.CoreWebView2.Navigate(m_url);
            }
        }
    }
}

WebView2SampleApp.zip

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.