C#C
C#3y ago
Thalnos

❔ Auth in WebView2 Control - WPF - Applying Headers

We offer the ability to interact with some external services in our WPF app via WebView controls:
click on "X" button -> open new window and set URL of WebView to access X service
click on "Y" button -> open new window and set URL of WebView to access Y Service

Each of those services requires auth. We use our Azure AD accounts from our organization to authenticate on all of those services.

I would like to implement SSO so that you only need to authenticate once rather than in each new opened WebView if possible.

Is there a way to add a header on every request from the WebView? so that I can add an Authorization header? I couldn't find a way to do so. Maybe I'm not on the right track, please let me know

public class WebViewWindow : Window {
   public WebViewWindow(string url){
      this.Browser.Source = new Uri(url);
      this.Browser.Headers.Add("Authorization", token); //can't find a way to do this part here
   }
}


<Window x:Class="AgentUI.WebViewWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
        mc:Ignorable="d"
        >
    <Grid>
        <wv2:WebView2 x:Name="Browser"/>
    </Grid>
</Window>
Was this page helpful?