FFImageLoading: How to add authentication?

感情迁移 提交于 2020-06-17 01:51:52

问题


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:


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



来源:https://stackoverflow.com/questions/41301212/ffimageloading-how-to-add-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!