Xamarin在UIWebView中打开自签名证书网页的方法

Sta*_*wer 6 ssl-certificate uiwebview xamarin.ios ios xamarin

正如描述的问题标题,我想在UIWebview(Xamarin.iOS)中打开自签名网页默认情况下,自签名网页不会加载到UIWebView中.

解决方案的重要要求:

  • 当我想将应用程序提交到Apple应用程序商店时,它应该被Apple接受(因此自定义NSUrlRequest不适合).
  • 它应该正确加载CSS和JavaScript.

我在stackoverflow上找到了一个可能的解决方案,但这适用于原生iOS. /sf/answers/816490321/ 我也想知道上述解决方案是否需要使用NSUrlConnectionDelegate登录.

理想的解决方案应该是用户可以使用UIWebView自行填写凭证.

有人可以为此提供Xamarin解决方案吗?我自己尝试过,但无法让它发挥作用.

在此先感谢您的帮助.

Mor*_*sen 2

我知道这是一篇很老的帖子,但这是一个非常有趣的问题,所以我不得不尝试一下。因此,如果您仍然需要它(很可能不需要)或者有人找到这篇文章,这里有一个支持自签名的原生 UIWebView 的移植版本。它可以用作常规 UIWebView,但它需要主机名作为附加参数,该参数应该是应禁用证书检查的页面的主机名。

public class InsecureWebView : UIWebView, INSUrlConnectionDataDelegate, IUIWebViewDelegate
{
    public InsecureWebView(string baseUrl) : base()
    {
        Setup (baseUrl);
    }

    public InsecureWebView(CoreGraphics.CGRect rect, string baseUrl) : base(rect)
    {
        Setup (baseUrl);
    }

    public InsecureWebView(NSCoder coder, string baseUrl) : base(coder)
    {
        Setup (baseUrl);
    }

    public InsecureWebView(NSObjectFlag t, string baseUrl) : base(t)
    {
        Setup (baseUrl);
    }

    public InsecureWebView(IntPtr handler, string baseUrl) : base(handler)
    {
        Setup (baseUrl);
    }

    string baseUrl = null;
    bool authenticated;
    NSUrlRequest failedRequest;

    private void Setup(string baseUrl)
    {
        this.Delegate = this;
        this.baseUrl = baseUrl;
    }


    [Foundation.Export ("webView:shouldStartLoadWithRequest:navigationType:")]
    public bool ShouldStartLoad (UIKit.UIWebView webView, Foundation.NSUrlRequest request, UIKit.UIWebViewNavigationType navigationType)
    {
        var result = authenticated;
        if (!authenticated) {
            failedRequest = request;
            NSUrlConnection.FromRequest (request, this);
        }
        return result;
    }

    [Foundation.Export ("connection:willSendRequestForAuthenticationChallenge:")]
    public void WillSendRequestForAuthenticationChallenge (NSUrlConnection connection, NSUrlAuthenticationChallenge challenge)
    {
        if (challenge.ProtectionSpace.AuthenticationMethod == NSUrlProtectionSpace.AuthenticationMethodServerTrust) {
            var baseUrl = new NSUrl (this.baseUrl);
            if (challenge.ProtectionSpace.Host == baseUrl.Host) {
                challenge.Sender.UseCredential (NSUrlCredential.FromTrust (challenge.ProtectionSpace.ServerSecTrust), challenge);
            }
        }
        challenge.Sender.ContinueWithoutCredential (challenge);
    }

    [Foundation.Export ("connection:didReceiveResponse:")]
    public void DidReceivedResponse(NSUrlConnection connection, NSUrlResponse response)
    {
        authenticated = true;
        connection.Cancel ();
        LoadRequest (failedRequest);
    }
}
Run Code Online (Sandbox Code Playgroud)