Beispiel #1
0
        public async Task <IActionResult> UploadImageAsync([Required] IFormFile file,
                                                           [FromServices] IUserDirectoryBlobProvider blobProvider,
                                                           [FromServices] UserManager <User> userManager,
                                                           CancellationToken token)
        {
            var userId = userManager.GetLongUserId(User);
            Uri uri;

            try
            {
                uri = await blobProvider.UploadImageAsync(userId, file.FileName, file.OpenReadStream(), file.ContentType, token);
            }
            catch (ArgumentException)
            {
                ModelState.AddModelError("x", "not an image");
                return(BadRequest(ModelState));
            }

            if (uri == null)
            {
                ModelState.AddModelError("x", "not an image");
                return(BadRequest(ModelState));
            }
            var imageProperties = new ImageProperties(uri, ImageProperties.BlurEffect.None);
            var url             = Url.ImageUrl(imageProperties);
            var fileName        = uri.AbsolutePath.Split('/').LastOrDefault();
            var command         = new UpdateUserImageCommand(userId, url, fileName);
            await _commandBus.DispatchAsync(command, token);

            return(Ok(url));
        }
Beispiel #2
0
        public async Task <ActionResult <ReturnSignUserResponse> > GoogleSignInAsync(
            [FromBody] GoogleTokenRequest model,
            [FromServices] IGoogleAuth service,
            [FromServices] IUserDirectoryBlobProvider blobProvider,


            [FromServices] TelemetryClient logClient,
            [FromServices] IHttpClientFactory clientFactory,
            CancellationToken cancellationToken)
        {
            var result = await service.LogInAsync(model.Token, cancellationToken);

            _logger.Info($"received google user {result}");
            if (result == null)
            {
                logClient.TrackTrace("result from google is null");
                ModelState.AddModelError("Google", _localizer["GoogleNoResponse"]);
                return(BadRequest(ModelState));
            }

            var result2 = await _signInManager.ExternalLoginSignInAsync("Google", result.Id, true, true);

            if (result2.Succeeded)
            {
                return(ReturnSignUserResponse.SignIn());
                //return new ReturnSignUserResponse(false);
            }

            if (result2.IsLockedOut)
            {
                logClient.TrackTrace("user is locked out");
                ModelState.AddModelError("Google", _loginLocalizer["LockOut"]);
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByEmailAsync(result.Email);

            if (result2.IsNotAllowed && user != null && await _userManager.IsLockedOutAsync(user))
            {
                ModelState.AddModelError("Google", _loginLocalizer["LockOut"]);
                return(BadRequest(ModelState));
            }
            if (user == null)
            {
                var country = await _countryProvider.GetUserCountryAsync(cancellationToken);

                user = new User(result.Email,
                                result.FirstName, result.LastName,
                                result.Language, country)
                {
                    EmailConfirmed = true
                };


                var result3 = await _userManager.CreateAsync(user);

                if (result3.Succeeded)
                {
                    if (!string.IsNullOrEmpty(result.Picture))
                    {
                        using var httpClient = clientFactory.CreateClient();
                        var message = await httpClient.GetAsync(result.Picture, cancellationToken);

                        await using var sr = await message.Content.ReadAsStreamAsync();

                        var mimeType = message.Content.Headers.ContentType;
                        try
                        {
                            var uri = await blobProvider.UploadImageAsync(user.Id, result.Picture, sr,
                                                                          mimeType.ToString(), cancellationToken);

                            var imageProperties = new ImageProperties(uri, ImageProperties.BlurEffect.None);
                            var url             = Url.ImageUrl(imageProperties);
                            var fileName        = uri.AbsolutePath.Split('/').LastOrDefault();
                            user.UpdateUserImage(url, fileName);
                        }
                        catch (ArgumentException e)
                        {
                            logClient.TrackException(e, new Dictionary <string, string>()
                            {
                                ["FromGoogle"] = result.Picture
                            });
                        }
                    }
                    await _userManager.AddLoginAsync(user, new UserLoginInfo("Google", result.Id, result.Name));

                    return(await MakeDecisionAsync(user, true, null, cancellationToken));
                }
                logClient.TrackTrace($"failed to register {string.Join(", ", result3.Errors)}");

                ModelState.AddModelError("Google", _localizer["GoogleUserRegisteredWithEmail"]);
                return(BadRequest(ModelState));
            }
            if (!user.EmailConfirmed)
            {
                user.EmailConfirmed = true;
                await _userManager.UpdateAsync(user);
            }

            await _userManager.AddLoginAsync(user, new UserLoginInfo("Google", result.Id, result.Name));

            return(await MakeDecisionAsync(user, true, null, cancellationToken));
        }