Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

707
Views
set Webview2 header or cookie

I want to set customized header for all requests that Webview2 makes. Please help. Basically I want to load website in webview itself so it is needed that I receive my header on all requests.

MainWindow.xaml.cs

using Microsoft.Web.WebView2.Core;
using System;
using System.Windows;

namespace O2C
{
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        private void webView_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
        {
            webView.CoreWebView2.Settings.UserAgent = "O2C-Web";
            webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
            webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
        }

        private void WebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
        {
            e.RequestHeaders.SetHeader("X-Authorization", "My Auth");
        }
    }
}
e.RequestHeaders.SetHeader("X-Authorization", "My Auth");

I have found the correct event which will work for the requirement but I don't know how to call that event from xaml file or from .cs file. Following docs say thaat this event will work for the requirement.

https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.webresourcerequested?view=webview2-dotnet-1.0.864.35

BUT how to hook that event and from where I don't know.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I Solved it after looking at many places.

If you want the WebResourceRequested Event then you must register the Filter that will intercept all requests for all resources. Then and then it will work or called.

I am posting whole code here:

private void webView_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
      //Following line is MUST if you want to use WebResourceRequested Event
      webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
      webView.CoreWebView2.Settings.UserAgent = "MY-AGENT";
      webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
      webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
      webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
}

private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
{
      e.Request.Headers.SetHeader("X-Authorization", "My Auth");
}

Thanks all for support and ideas. Hope this solution helps.

over 4 years ago · Santiago Trujillo Report

0

That event WebResourceRequested is on the CoreWebView2 class and you can find your CoreWebView2 as a property on the WebView2 class after the WebView2.CoreWebView2InitializationCompleted event fires.

        private void webView_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
        {
            if (e.IsSuccess)
            {
                webView.CoreWebView2.Settings.UserAgent = "O2C-Web";
                webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
                webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;

                webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
            }
        }

        private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
        {
            e.Request.Headers.SetHeader("X-Authorization", "My Auth");
        }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!