question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Intercepting requests from BlazorWebView

See original GitHub issue

Description

In the documentation surrounding Blazor Hybrid apps with .NET MAUI it is stated that it not recommended to expose the access token to JavaScript running inside of the WebView. Instead it is suggested that network requests are intercepted so that the access token can be injected by the native app. This indeed seems to me to be the most secure way of doing it.

However it doesn’t seem there’s a good way to do this with the current API’s. I’ve managed to get something to work on Android by wrapping the WebViewClient that is injected by the BlazorWebView control and overriding a couple of methods there, but this feels brittle as the WebViewClient provided by Blazor might override additional methods in the future. On iOS I haven’t found a solution that works so far although I haven’t looked into it deeply enough yet.

Public API Changes

var webView = new BlazorWebView();
webView.OnRequest += (sender, args) =>
{
  args.RequestHeaders.Add("Authorization", "Bearer " + token);
}

Intended Use-Case

It would be great if the BlazorWebView had some API that would allow us to intercept requests and inject additional request headers depending on the destination of the request for example. This would allow non-Blazor JavaScript to run inside of a Blazor Hybrid app while communicating with a backend somewhere that requires authentication.

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:8
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jmezachcommented, Feb 6, 2023

@mcl-sz Rather than replacing the WebViewClient, try wrapping it instead. So I created a derived WebViewClient class that accepts a WebViewClient as a constructor parameter and in the ShoudlInterceptRequest() method I do my custom logic if needed and call the inner WebViewClient (if needed). That way the Blazor specific logic should still work.

2reactions
jmezachcommented, Jan 23, 2023

@inetzo I did manage to get something working by injecting a custom WebViewClient on Android and a custom UrlSchemeHandler on iOS through the BlazorWebViewInitialized and BlazorWebViewInitializing. Unfortunately I found out that on Android you can implement the ShouldInterceptRequest method, but it doesn’t give you access to the request body so you cannot perform a full request which is quite limiting IMHO. However, that is a limitation of the Android platform, not so much of .NET MAUI nor Blazor.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Restricting network access for BlazorWebview : r/dotnetMAUI
I'm developing a Blazor Hybrid app, but this should generally apply to BlazorWebviews: Is there a way to restrict which urls the Webview...
Read more >
NET MAUI Blazor Hybrid how to intercept open in new tab
Hi @Andrew Denha , it seems that your main purpose is to open a MAUI page in BlazorWebview when the Blazor action is...
Read more >
ASP.NET Core Blazor Hybrid authentication and ...
Intercept the outgoing network request in code. Replace the temporary token with the real token and confirm that the destination of the request...
Read more >
BlazorWebView Android Tutorial.
BlazorWebView will intercept URLs loaded from the framework:// scheme and present the content directly to the native operating webview.
Read more >
BlazorWebView iOS Tutorial.
BlazorWebView will intercept URLs loaded from the framework:// scheme and present the content directly to the native operating webview.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found