Every now and again you find a topic where you say "I know I'm not the first to ask this" but can't find any current conversation or resolution. This seems to be one of those. I'm not a web guy, so maybe this is something everyone knows through growing up in it but just seems to be uncovered for 2018 and Xamarin Forms >= 2.5
All I want to do is have my app open a web page that wants basic HTTP authentication.
There are countless articles from 2014 on doing this.
https://forums.xamarin.com/discussion/42918/how-to-access-protected-resource-using-xamarin-webview-basic-authentication
The implementation here is really straightforward with a nice username and password property you can set in XAML even.
And a good Nuget for an authenticating webview
https://github.com/TheRealAdamKemp/AuthenticatedWebView/issues/3
However all of these answers I'm finding for Xamarin forms less than 2.5. Those same bits of solution do not work with the WebView
of 2.5 and later. When I say 'Does not work' I mean two things mostly:
1) The code makes use of obsoleted constructor and method syntax. That seems to have been an easy hurdle but maybe I'm wrong.
2) The method needed for passing the credentials (OnReceivedHttpAuthRequest
) is never hit in XF3.3. So if the WebView
doesn't run this override you never get to send the username/password.
I can't believe I'm the first person to want to do this basic function. Or felt pain when XF1.3 was replaced by 2.5 and it stopped working.
I have to believe I just don't know some basic thing that everyone else knows like "Use the blah blah control" or whatever. But I'm in this a week now and can't seem to make the working solutions I've found from 1.3 work in modern 3.3
Here's my updated version of code from the 2015 thread - maybe someone with more web experiences spots something obvious.
using System;
using Android.Content;
using Android.Webkit;
using SysUtilityPFX.Controls;
using SysUtilityPFX.Droid.Common;
using Xamarin.Forms;
using WebView = Xamarin.Forms.WebView;
[assembly: ExportRenderer(typeof(AuthWebView), typeof(AuthWebViewRenderer))]
namespace SysUtilityPFX.Droid.Common
{
public class AuthWebViewRenderer : Xamarin.Forms.Platform.Android.WebViewRenderer
{
AuthWebViewClient _authWebClient = null;
public AuthWebViewRenderer(Context context) : base(context)
{
var breakpointable = 5;
}
/// <summary>Syntax obsolete since XamForms 2.5
///
/// </summary>
/// <param name="e"></param>
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (_authWebClient == null)
{
_authWebClient = new AuthWebViewClient("admin", "admin");//Testing to make it work then make it better afterwards.
//_authWebClient = new AuthWebViewClient(AuthWebView.Username, AuthWebView.Password);//Error Object reference for static
}
Control.SetWebViewClient(_authWebClient);
}
}
public class AuthWebViewClient : WebViewClient
{
private string Username
{
get;
}
private string Password
{
get;
}
public AuthWebViewClient(string username, string password)
{
Username = username;
Password = password;
}
//Never hits breakpoint
public override void OnReceivedHttpAuthRequest(global::Android.Webkit.WebView view, HttpAuthHandler handler, string host, string realm)
{
handler.Proceed(
Username,
Password);
}
[Obsolete("deprecated")]
public override bool ShouldOverrideUrlLoading(global::Android.Webkit.WebView view, string url)
{
view.LoadUrl(url);
return true;
}
}
}
/* References
https://forums.xamarin.com/discussion/42918/how-to-access-protected-resource-using-xamarin-webview-basic-authentication
https://www.ozkary.com/2018/06/xamarin-android-webview-authentication.html
https://xamarinexplorer.com/2018/01/20/adding-authorization-header-in-web-view/
iOS only
https://github.com/TheRealAdamKemp/AuthenticatedWebView/issues/3
XF 1.3 version works great. Not in 2018 with XF3.3
*/