How to set cookies to WebView2 control in c#?

remya Paul 96 Reputation points
2020-10-29T10:41:51.553+00:00

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?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,832 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
{count} votes

Accepted answer
  1. remya Paul 96 Reputation points
    2020-11-18T07:24:10.12+00:00

    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

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Leon Laude 85,666 Reputation points
    2020-10-29T11:47:52.767+00:00

    Hi @remya Paul ,

    C# is currently not supported in the Q&A forums, the supported products are listed over here https://learn.microsoft.com/en-us/answers/products (more to be added later on).

    You can ask the experts in the dedicated C# forum over here:
    https://social.msdn.microsoft.com/Forums/en-US/home?forum=csharpgeneral

    ----------

    (If the reply was helpful please don't forget to upvote or accept as answer, thank you)

    Best regards,
    Leon


  2. Tianyu Sun-MSFT 27,356 Reputation points Microsoft Vendor
    2020-11-06T07:49:21.853+00:00

    Hi @remya Paul ,

    Thank you for your feedback.

    From this API Notes, which indicates HttpRequestHeaders.Add => CoreWebView2HttpRequestHeaders.SetHeader, I think you may need to change to use e.Request.Headers.SetHeader(XXXX,XXXX) to set cookies to WebView2 control.

    37908-1.png

    Sincerely,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    0 comments No comments