在 Xamarin WebView 中将 JWT 令牌作为 header 传递

Pass JWT token as header in Xamarin WebView

我正在使用 Xamarin 表单 WebView 控件来显示使用身份验证 JWT 令牌的页面。我无法在 Microsoft 站点或任何博客中找到执行此操作的任何示例。

我找到的最接近的答案是为每个平台(iOS 和 Droid)中的控件创建一个渲染器。但是我不确定应该在哪个事件上覆盖请求以及可以传递 auth header 的格式。感谢任何帮助。

您可以试试下面的方法:

对于Android:

public class FormsWebViewRenderer : ViewRenderer<Xamarin.Forms.WebView, Android.Webkit.WebView>
{
    Android.Content.Context _localContext;

    public FormsWebViewRenderer(Context context) : base(context)
    {
        _localContext = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
    {
        base.OnElementChanged(e);

        Dictionary<string, string> headers = new Dictionary<string, string>
        {
            ["A-custom-header"] = "a custom value"
        };

        Android.Webkit.WebView webView = Control as Android.Webkit.WebView;

        if (Control == null) {
            webView = new Android.Webkit.WebView(_localContext);
            SetNativeControl(webView);
        }

        webView.Settings.JavaScriptEnabled = true;

        webView.Settings.BuiltInZoomControls = true;
        webView.Settings.SetSupportZoom(true);

        webView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
        webView.ScrollbarFadingEnabled = false;

        webView.SetWebViewClient(new FormsWebViewClient(headers));
        UrlWebViewSource source = Element.Source as UrlWebViewSource;
        webView.LoadUrl(source.Url, headers);
    }
}

public class FormsWebViewClient : Android.Webkit.WebViewClient
{
    public Dictionary<string, string> headers { get; set; }

    public FormsWebViewClient(Dictionary<string, string> requestHeaders)
    {
        headers = requestHeaders;
    }

    public override void OnPageStarted(Android.Webkit.WebView view, string url, Android.Graphics.Bitmap favicon)
    {
        base.OnPageStarted(view, url, favicon);
        System.Diagnostics.Debug.WriteLine("Loading website...");
    }

    public override void OnPageFinished(Android.Webkit.WebView view, string url)
    {
        base.OnPageFinished(view, url);
        System.Diagnostics.Debug.WriteLine("Load finished.");
    }

    public override void OnReceivedError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceError error)
    {
        base.OnReceivedError(view, request, error);
    }
}

对于ios:

public class FormsWebViewRenderer : ViewRenderer<WebView, UIWebView>
{
    protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);

        var webView = Control as UIWebView;

        if (webView == null) {
            webView = new UIWebView();
            SetNativeControl(webView);
        }

        webView.ScalesPageToFit = true;

        webView.LoadStarted += (object sender, System.EventArgs evtArgs) => {
            System.Diagnostics.Debug.WriteLine("Loading...");
        };

        webView.LoadFinished += (object sender, System.EventArgs evtArgs) => {
            System.Diagnostics.Debug.WriteLine("Load finished.");
        };

        if (e.NewElement != null) {
            UrlWebViewSource source = (Xamarin.Forms.UrlWebViewSource)Element.Source;
            var webRequest = new NSMutableUrlRequest(new NSUrl(source.Url));
            var headerKey = new NSString("A-custom-header");
            var headerValue = new NSString("a custom value");
            var dictionary = new NSDictionary(headerKey, headerValue);

            webRequest.Headers = dictionary;

            this.Control.LoadRequest(webRequest);
        }

    }
}