2016-12-15 9 views
2

私は、SSLで保護された私たちのアプリケーションでアップロードされたユーザー画像のオンラインストアを使用しています。私はWebClientを証明書を添付して使用しているので、アップロードはうまく動作します。しかし、私がXamarin.Forms.Imageコンポーネントを使用しようとしているときなど、ソースが "https://blabla.com/upload/image123.jpg"に設定されていると、画像をAndroidに読み込むことができません。 iOSでは、これはSSL接続を処理するカスタムNSUrlProtocolを持っているので動作します。Xamarin.Forms SSLを使用した画像ソース

var image = new Image(); 

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

は、Webクライアントの場合、私はHttpWebRequest.ClientCertificatesにX509Certificate2(秘密鍵とパスワード)を添付して、それが動作します。しかし、ImageLoaderSourceHandlerの背後にあるどのようなローディングメカニズムにも、私はその証明書をどのように提供することができないのか分かりません。

Androidでこの作業を行うにはどうすればよいですか?

+0

ソースを追加する方法は?コードを共有できますか? –

+0

'Image.Source = ImageSource.FromUri(" https://blabla.com/upload/image123.jpg ")の意味について –

+0

文字列を指定すると、ImageSourceConverterはそれを正確に提供します。私が得られないことは、ImageLoaderSourceHandlerをSSL保護されたURLからロードする方法です。 – Florian

答えて

2

だから、私は自分の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; 
     } 
    } 
} 
+0

こんにちは@Florian 私に教えてください** SecuredUriImageSource **クラスコンテンツ? ** iOSハンドラ**を共有します。 –

+0

これは実際にはとても簡単です - 私は答えを更新しました。 – Florian

+0

ありがとうございました!これはうまくいきました... –

関連する問題