public async Task <GravatarProxyResult> GetAvatarOrNullAsync(string username, int imageSize) { if (!_features.IsGravatarProxyEnabled()) { return(null); } var user = _users.GetAll().FirstOrDefault(u => u.Username == username); if (user == null) { _logger.LogWarning("Could not find an account with username {Username}", username); return(null); } try { var emailAddress = user.EmailAddress ?? user.UnconfirmedEmailAddress; var useEnSubdomain = _features.ProxyGravatarEnSubdomain(); var url = GravatarHelper.RawUrl(emailAddress, imageSize, useEnSubdomain); // The response will be disposed when the caller disposes the content stream. var client = _httpClientFactory.CreateClient(GravatarHttpClientName); var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead); response.EnsureSuccessStatusCode(); string contentType = null; if (response.Content.Headers.TryGetValues(HttpContentTypeHeaderName, out var contentTypes)) { contentType = contentTypes.FirstOrDefault(); } return(new GravatarProxyResult( await response.Content.ReadAsStreamAsync(), contentType ?? HttpContentTypeDefaultValue)); } catch (Exception e) { _logger.LogError(0, e, "Unable to fetch profile picture for user {Username}", username); return(null); } }