FFImageLoading: How to add authentication?

1.5k views Asked by At

I'm trying to use FFImageLoading in my Xamarin.iOS and Xamarin.Android apps to load images from url. All the url's require authentication..

Also I'm using modernhttpclient for all other Rest Calls. For modernhttpclient this is how I'm adding authentication

var cookieHandler = new NativeCookieHandler ();
var messageHandler = new NativeMessageHandler (false, false, cookieHandler);
cookieHandler.SetCookies (RestApiPaths.cookies);
using (var client = new HttpClient (messageHandler)) {
client.DefaultRequestHeaders.TryAddWithoutValidation ("User-Agent", GetUserAgent (platform));
var r = await client.GetAsync (new Uri (url));

and it is working very well. I did same thing for FFImageLoading,

var cookieHandler = new NativeCookieHandler ();
var messageHandler = new NativeMessageHandler (false, false, cookieHandler);
cookieHandler.SetCookies (RestApiPaths.cookies);
var client = new HttpClient (messageHandler);
client.DefaultRequestHeaders.TryAddWithoutValidation ("User-Agent", RestApiPaths.GetUserAgent (Constants.__IOS__));

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

ImageService.Instance.LoadUrl (url).Into (ArticleImageView);

but unfortunately, it is not working. No image get loaded. I know it is problem with authentication. I tried with another url which does not require authentication and its working. Can someone please tell what should I do to get it working.

EDIT:

I tried this from Authentication-Support

public class AuthenticatedHttpImageClientHandler : HttpClientHandler
{

    public AuthenticatedHttpImageClientHandler()
    {

    }

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.Add("Cookie", RestApiPaths.cookie);
        request.Headers.Add("User-Agent", RestApiPaths.GetUserAgent(Constants.__IOS__));
        return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
    }
}

then:

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

but still not working..

1

There are 1 answers

2
Drunken Daddy On BEST ANSWER

Here's how I did it

ImageService.Instance.Initialize(new Configuration {
    HttpClient = NetworkHelper.GetAuthenticatedHttpClient(Constants.__IOS__)
});
ImageService.Instance.LoadUrl(url).Into(imageView);

NetworkHelper.GetAuthenticatedHttpClient returns the an HttpClient with the required headers and auth token in my case.

public static HttpClient GetAuthenticatedHttpClient(int platform)
{
     HttpClient client = new HttpClient(messageHandler);
     client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", GetUserAgent(platform));
     client.DefaultRequestHeaders.TryAddWithoutValidation("X-Time-Zone", timezone);
     client.DefaultRequestHeaders.TryAddWithoutValidation("appVersion", CommonHelper.AppVersion);
     client.DefaultRequestHeaders.TryAddWithoutValidation("X-Auth", GetAuthToken());
     return client;
}

You only have to do Instance.Initialize only once