Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

C# Authorize to download image Xamarin.forms

$
0
0

I have 2 fields in my model (Title, Picture)

                <ImageCell Height="100"
                 Text="{Binding Title}" 
                 ImageSource="{Binding Picture, Converter={StaticResource PictureURL}}">
                </ImageCell>

Field Picture is a string value which is a part of url of image. So it uses with IValueConverter to get string for download the image.
But on downloading step it has Unauthorized status
[0:] HTTP Request: Could not retrieve h_ttps://servername/api/File/L2NvbXBhbnluZXdzL0xpdXNpbmVzc2ZtLmpwZw2, status code Unauthorized

I have code to authorize on server:

            var handler = new NativeMessageHandler();
            HttpClient client = new HttpClient(handler);
            var authData = string.Format("{0}:{1}", Settings.Username, Settings.Password);
            var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);

In cases when I use FFImageLoading I will add this:

            ImageService.Instance.Initialize(new Configuration
            {
                HttpClient = client
            });

But how I can bind my autorization to this case when I use IValueConvberter for imagesource Picture ?

Full code of xaml.cs page.

namespace Project.Mobile.Client.Portable.Views.Names
{
      public class PicturesURLConverter : IValueConverter
     {

          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
                return Settings.ServerUrl + "/api/File/" + (string)value;
           }

          public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
          {
             throw new NotImplementedException();
          }
     }
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NamesListPage : ContentPage
    {
        public ObservableCollection<Models.Names> items { get; set; }

    public NewPage()
    {
        items = new ObservableCollection<Models.Names>();
        this.BindingContext = this;
        InitializeComponent();

        var handler = new NativeMessageHandler();
        HttpClient client = new HttpClient(handler);
        var authData = string.Format("{0}:{1}", Settings.Username, Settings.Password);
        var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
       //somce code to bind autorization to imagesouce Picture for download

        // Disabling selection
        Lst.ItemSelected += (sender, e) => {
            ((ListView)sender).SelectedItem = null;
        };

        Lst.Refreshing += (sender, e) => {
            LoadUsersData();
        };
        LoadUsersData();
    }

    public async void LoadUsersData()
    {
        Lst.IsRefreshing = true;

        var names = await App.Database.Names.GetItemsAsync();
        items.Clear();


        foreach (var item in names)
            items.Add(item);

        Lst.IsRefreshing = false;
    }

    public async void OnItemTapped(object sender, ItemTappedEventArgs e)
    {
        NamesReadPage readPage = new NamesReadPage();
        readPage.BindingContext = e.Item as Models.Names;

        await Navigation.PushAsync(readPage);
    }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles