.NET MAUI WebView 在基本身份验证网页上自动登录

问题描述 投票:0回答:2

我构建了一个 .NET MAUI 应用程序,可以显示我公司中的一些网页。

这些网页不应该从应用程序外部访问,因此我向它们添加了基本身份验证(在 Win2019 上使用 IIS)。

现在,当有人尝试使用该 URL 连接到网页时,系统会提示他们输入凭据,因此他们无法看到内容。

我现在希望我的 MAUI 应用程序能够使用硬编码凭据在网页中自动进行身份验证,以便用户仅在使用 MAUI 应用程序时才能看到内容。

我为此使用 WebView。

如有任何帮助,我们将不胜感激。

.net iis webview basic-authentication maui
2个回答
0
投票

如果您使用访问令牌进行身份验证(例如:在 Xamarin iOS 应用程序中显示仅限会员的 DotNetNuke 网页),则

WebViewRenderer
类可以访问该令牌并向 Web 请求添加 Authorization 标头。

您可以参考以下代码:

protected NSMutableUrlRequest CreateAuthenticatedWebRequest()
{
    var headerKey = new NSString("Authorization");
    var headerValue = new NSString("Bearer " + App.User.AccessToken);
    var headersDictionary = new NSDictionary(headerKey, headerValue);

    UrlWebViewSource source = (Xamarin.Forms.UrlWebViewSource)Element.Source;
    var webRequest = new NSMutableUrlRequest(new NSUrl(_originalSourceUrlValue));
    webRequest.Headers = headersDictionary;

    return webRequest;
}

然后在

OnElementChanged()
方法中,我们可以通过代码使用经过身份验证的Web请求:

 Control.LoadRequest(CreateAuthenticatedWebRequest()).

虽然它是在xamarin表单上,但我相信在maui中的实现是类似的。

您也可以参考这个帖子:Android Webview通过设置token cookie自动登录https网站


0
投票

对于那些寻找如何使用不记名令牌执行此操作的人,这对我在 .NET MAUI 中有用

using System;
using NavistarOCCAppMAUI.Common;
#if IOS
using Foundation;
using UIKit;
using WebKit;
#endif
namespace NavistarOCCAppMAUI.Handlers
{
    public class AuthenticatedWebView : WebView
    {
        static AuthenticatedWebView()
        {
            Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping(nameof(IWebView.Source), async (handler, view) =>
            {
                if (view is AuthenticatedWebView webView)
                {
                    var url = webView.Url!;
                    var token =await  LoginSecureStorage.Instance.GetAuthAccessToken();
#if ANDROID
                var headers = new Dictionary<string, string>
                {
                    ["Authorization"] = token
                };
                handler.PlatformView.LoadUrl(url, headers);
#elif IOS
            var webRequest = new NSMutableUrlRequest(new NSUrl(url));
            var headerKey = new NSString("Authorization");
            var headerValue = new NSString(token);
            var dictionary = new NSDictionary(headerKey, headerValue);
            webRequest.Headers = dictionary;
            var webViewDelegate = new WebViewDelegate();
            handler.PlatformView.NavigationDelegate = webViewDelegate;

            handler.PlatformView.LoadRequest(webRequest as NSUrlRequest);
#else
            throw new NotImplementedException();
#endif
            }

        });
    }

    public static readonly BindableProperty TokenProperty = BindableProperty.Create("Token", typeof(string), typeof(AuthenticatedWebView), "");

    public string? Token
    {
        get => GetValue(TokenProperty) as string;
        set => SetValue(TokenProperty, value);
    }

    public static readonly BindableProperty UrlProperty = BindableProperty.Create("Url", typeof(string), typeof(AuthenticatedWebView), "");
    public string? Url
    {
        get => GetValue(UrlProperty) as string;
        set => SetValue(UrlProperty, value);
    }
}
#if IOS
///on iOS tell WkWebview to use credentials
public class WebViewDelegate : WKNavigationDelegate
{
    public override void DidReceiveAuthenticationChallenge(WKWebView webView, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
    {
        completionHandler(NSUrlSessionAuthChallengeDisposition.UseCredential, new NSUrlCredential(challenge.ProtectionSpace.ServerSecTrust));

        return;
    }
}
#endif
}
© www.soinside.com 2019 - 2024. All rights reserved.