Xamarin.Forms Image.Source与SSL [英] Xamarin.Forms Image.Source with SSL

查看:91
本文介绍了Xamarin.Forms Image.Source与SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用在线商店存储通过SSL保护的通过我们的应用上传的用户图像.当我使用附有证书的WebClient时,上传效果很好.但是当我尝试使用Xamarin.Forms.Image组件时,例如并将源"设置为" https://blabla.com/upload/image123.jpg "该图像无法在Android上加载.在iOS上,这是因为我有一个自定义的NSUrlProtocol,它可以处理SSL连接.

I'm using an online store for user images uploaded with our App secured by SSL. The upload works all well as I'm using the WebClient with the certificate attached. But when I'm trying to use the Xamarin.Forms.Image component e.g. with the Source set to "https://blabla.com/upload/image123.jpg" the image can't be loaded on Android. On iOS this works as I've got a custom NSUrlProtocol which handles the SSL connection.

var image = new Image();

//will use ImageLoaderSourceHandler 
image.Source = "https://blabla.com/upload/image123.jpg";

如果是WebClient,我将X509Certificate2(专用密钥和密码)附加到HttpWebRequest.ClientCertificates,它可以工作.但是我对如何向ImageLoaderSourceHandler背后的任何加载机制提供该证书一无所知.

In case of a WebClient I attach the X509Certificate2 (private key and password) to HttpWebRequest.ClientCertificates and it works. But I'm lost on how I can provide that certificate to whatever loading mechanism is behind ImageLoaderSourceHandler.

如何在Android上执行此操作?

How can I make this work on Android?

推荐答案

所以我最终设置了自己的SecuredUriImageSource:

So I ended up setting up my own SecuredUriImageSource:

var image = new Image();

//will use SecuredImageLoaderSourceHandler  
image.Source = new SecuredUriImageSource ("https://blabla.com/upload/image123.jpg");

使用此自定义处理程序加载图像,而WebClientEx将实际的证书附加到连接.

Which uses this custom handler to load the image, while WebClientEx attaches the actual Certificate to the connection.

[assembly: ExportImageSourceHandler(typeof(SecuredUriImageSource), typeof(SecuredImageLoaderSourceHandler))]
namespace Helpers
{
    public class SecuredUriImageSource : ImageSource 
    {
        public readonly UriImageSource UriImageSource = new UriImageSource();

        public static SecuredUriImageSource FromSecureUri(Uri uri)
        {
            var source = new SecuredUriImageSource ();

            source.UriImageSource.Uri = uri;

            return source;
        }
    }

    public class SecuredImageLoaderSourceHandler : IImageSourceHandler
    {
        public async Task<Bitmap> LoadImageAsync(ImageSource imagesource, Android.Content.Context context, CancellationToken cancelationToken = default(CancellationToken))
        {
            var imageLoader = imagesource as SecuredUriImageSource;

            if (imageLoader != null && imageLoader.UriImageSource.Uri != null)
            {
                var webClient = new WebExtensions.WebClientEx();
                var data = await webClient.DownloadDataTaskAsync(imageLoader.UriImageSource.Uri, cancelationToken).ConfigureAwait(false);
                using (var stream = new MemoryStream(data) )
                    return await BitmapFactory.DecodeStreamAsync(stream).ConfigureAwait(false);
            }

            return null;
        }
    }
}

这篇关于Xamarin.Forms Image.Source与SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆