简体   繁体   中英

set Webview2 header in Wpf application

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

<DockPanel>
    <wv2:WebView2 Name="webView"
            Source="http://localhost/serverinfo"
            CoreWebView2InitializationCompleted="webView_CoreWebView2InitializationCompleted"
            ZoomFactor="1.2"
            NavigationStarting="WebView_NavigationStarting"      
    />
</DockPanel>

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");

Above line is correct but the event is not correct as it works if new URL is there, but if same URL is there it doesn't work.

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.

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");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM