locked
How to set cookies to WebView2 control in c#? RRS feed

  • Question

  • How to set the cookies in Webview2 control in c#?

    Here is how i am navigating the url into WebView2 control.
    WebView.CoreWebView2.Navigate("URL Name");

    I tried using the 'WebResourceRequested' event but there is no way to add headers into the request.
    WebView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
    private void WebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
    {
    strNavigationURL = e.Uri;
    e.RequestHeaders.????
    }

    Since I have read that in some other forum that this is a known bug of WebView2, and hopefully all waiting to see that is been resolved in WebView2 SDK 1.0.674-prerelease.

    But i could not find any solution in WebView2 1.0.674 -Prerelease. Can someone help me in this regard?

    Since i need to pass the authorization token to this URL, Can someone help me to add the header value to the request?

    Friday, October 30, 2020 4:07 AM

Answers


  • Thank you for the details.

    I am able to pass the cookies this way.

    on the form constructor initialised
       WebView.CoreWebView2Ready += WebView_CoreWebView2Ready1;

    Then at the CoreWebView2Ready event initialized WebResourceRequested event.
    private void WebView_CoreWebView2Ready1(object sender, EventArgs e)
            {
                WebView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
                WebView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
            }
    Then at the CoreWebView2_WebResourceRequested event i have set the cookies
     private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
            {
                // Set and access the header            
                e.Request.Headers.SetHeader("Cookie", XXXXX);
                var addedDate = e.Request.Headers.GetHeader("Cookie");
            }

    Hope this will be useful for someone
    Wednesday, November 18, 2020 7:27 AM

All replies

  • Hi,

    Maybe you can try to add a Filter, and then add a header in the WebResourceRequested event.

    Something like this:

            private void Form1_Load(object sender, EventArgs e)
            {
                webView21.CoreWebView2Ready += WebView2_CoreWebView2Ready;
            }
    
            private void WebView2_CoreWebView2Ready(object sender, EventArgs e)
            {
                try
                {
                    var coreWebView2 = webView21.CoreWebView2;
                    coreWebView2.AddWebResourceRequestedFilter("url", CoreWebView2WebResourceContext.All);
                    coreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
                  
                    coreWebView2.Navigate("url");
                }
                catch (Exception exception)
                {
                    MessageBox.Show(this, exception.ToString());
                    throw;
                }
            }
    
            private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
            {
                e.Request.Headers.SetHeader("name", "value");
            }

    But I am not sure if it can solve your problem.

    If this code is not useful, I think you'd better ask them questions in their GitHub repository, they should be very familiar with it.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 30, 2020 8:48 AM

  • Thank you for the details.

    I am able to pass the cookies this way.

    on the form constructor initialised
       WebView.CoreWebView2Ready += WebView_CoreWebView2Ready1;

    Then at the CoreWebView2Ready event initialized WebResourceRequested event.
    private void WebView_CoreWebView2Ready1(object sender, EventArgs e)
            {
                WebView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
                WebView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
            }
    Then at the CoreWebView2_WebResourceRequested event i have set the cookies
     private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
            {
                // Set and access the header            
                e.Request.Headers.SetHeader("Cookie", XXXXX);
                var addedDate = e.Request.Headers.GetHeader("Cookie");
            }

    Hope this will be useful for someone
    Wednesday, November 18, 2020 7:27 AM