【问题标题】:Xamarin.Forms Image.Source with SSLXamarin.Forms Image.Source 与 SSL
【发布时间】:2017-04-30 20:44:50
【问题描述】:

我正在使用在线商店来存储通过 SSL 保护的应用程序上传的用户图像。上传工作得很好,因为我正在使用带有证书的 WebClient。但是当我尝试使用 Xamarin.Forms.Image 组件时,例如源设置为“https://blabla.com/upload/image123.jpg”时,图像无法在 Android 上加载。在 iOS 上,这是可行的,因为我有一个自定义 NSUrlProtocol 来处理 SSL 连接。

var image = new Image();

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

如果是 WebClient,我将 X509Certificate2(私钥和密码)附加到 HttpWebRequest.ClientCertificates 并且它可以工作。但是我不知道如何将该证书提供给 ImageLoaderSourceHandler 背后的任何加载机制。

如何在 Android 上进行这项工作?

【问题讨论】:

  • 怎么加源码?能分享一下代码吗?
  • image.Source= ImageSource.FromUri("https://blabla.com/upload/image123.jpg")怎么样
  • 如果提供一个字符串,ImageSourceConverter 将提供该字符串。我没有得到的是如何让 ImageLoaderSourceHandler 从 SSL 安全的 Url 加载。
  • 您使用的是自签名证书吗?

标签: image ssl xamarin xamarin.forms imagesource


【解决方案1】:

所以我最终建立了自己的 SecuredUriImageSource:

var image = new Image();

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

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

[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;
        }
    }
}

【讨论】:

  • 嗨@Florian 你能给我SecuredUriImageSource课程内容吗?并分享 iOS 处理程序
  • 谢谢,我在网上看到的第一个自定义图像源的好例子
【解决方案2】:

我按照https://blog.xamarin.com/securing-web-requests-with-tls-1-2/ 中的设置,HTTPS 源刚刚开始加载。

编辑:

你可以打开“Android项目”-->“Android选项”-->“高级”的属性页,选择HttpClient Implementation为Managed code,下一个选项使用Native TLS 1.2+

【讨论】:

    【解决方案3】:

    我必须更新所有 Xamarin.Android 包才能使其正常工作

    【讨论】:

      【解决方案4】:

      您也可以使用StreamImageSource 或 ImageSource.FromStream(() => ...) 并提供您自己的自定义逻辑来提供图像流。

      var image = new Image();
      
      image.Source = new StreamImageSource() {
           Stream = async (CancellationToken cancellationToken) => {
               Stream stream = //...custom logic using your own HttpClient / WebClient for obtaining the data stream;
      
               return stream;
           }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-03
        • 1970-01-01
        相关资源
        最近更新 更多