Example #1
0
        public async Task <IActionResult> GetUserPicture(string id)
        {
            var entity = await userManager.FindByIdAsync(id);

            if (entity == null)
            {
                return(NotFound());
            }

            try
            {
                if (entity.IsPictureUrlStored())
                {
                    return(new FileStreamResult(await userPictureStore.DownloadAsync(entity.Id), "image/png"));
                }
            }
            catch
            {
                return(new FileStreamResult(new MemoryStream(AvatarBytes), "image/png"));
            }

            using (var client = new HttpClient())
            {
                var url = entity.PictureNormalizedUrl();

                if (!string.IsNullOrWhiteSpace(url))
                {
                    var response = await client.GetAsync(url);

                    if (response.IsSuccessStatusCode)
                    {
                        var contentType = response.Content.Headers.ContentType.ToString();

                        return(new FileStreamResult(await response.Content.ReadAsStreamAsync(), contentType));
                    }
                }
            }

            return(new FileStreamResult(new MemoryStream(AvatarBytes), "image/png"));
        }
Example #2
0
        public async Task <IActionResult> GetUserPicture(string id)
        {
            try
            {
                var entity = await userResolver.FindByIdOrEmailAsync(id);

                if (entity != null)
                {
                    if (entity.IsPictureUrlStored())
                    {
                        return(new FileStreamResult(await userPictureStore.DownloadAsync(entity.Id), "image/png"));
                    }

                    using (var client = new HttpClient())
                    {
                        var url = entity.PictureNormalizedUrl();

                        if (!string.IsNullOrWhiteSpace(url))
                        {
                            var response = await client.GetAsync(url);

                            if (response.IsSuccessStatusCode)
                            {
                                var contentType = response.Content.Headers.ContentType.ToString();

                                return(new FileStreamResult(await response.Content.ReadAsStreamAsync(), contentType));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", nameof(GetUser))
                             .WriteProperty("status", "Failed"));
            }

            return(new FileStreamResult(new MemoryStream(AvatarBytes), "image/png"));
        }