question

remyaPaul-8741 avatar image
0 Votes"
remyaPaul-8741 asked remyaPaul-8741 answered

How to set cookies to WebView2 control in c#?

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-wpfwindows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @remyaPaul-8741, which kind of project do you use? Do you use Windows Forms project or WPF (Windows Presentation Foundation) project?

0 Votes 0 ·

windows forms

0 Votes 0 ·
remyaPaul-8741 avatar image
0 Votes"
remyaPaul-8741 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLaude avatar image
0 Votes"
LeonLaude answered remyaPaul-8741 commented

Hi @remyaPaul-8741,

C# is currently not supported in the Q&A forums, the supported products are listed over here https://docs.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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TianyuSun-MSFT avatar image
0 Votes"
TianyuSun-MSFT answered TianyuSun-MSFT edited

Hi @remyaPaul-8741,

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.



1.png (20.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.