Esempio n. 1
0
        public async Task <TViewModel> GetAddressFromUser <TViewModel>(string id)
        {
            KeepFitUser user = null;

            try
            {
                user = await this.context
                       .Users
                       .Include(x => x.Address)
                       .ThenInclude(x => x.City)
                       .AsNoTracking()
                       .SingleOrDefaultAsync(x => x.Id == id);
            }
            catch (InvalidOperationException ex)
            {
                throw new ServiceException(string.Format(ExceptionMessages.UserLookupFailed, id), ex);
            }

            if (user == null)
            {
                throw new UserNotFoundException(string.Format(ExceptionMessages.UserLookupFailed, id));
            }

            var address = this.mapper.Map <TViewModel>(user.Address);

            return(address);
        }
        public async Task InvokeAsync(HttpContext context, IConfiguration configuration, UserManager <KeepFitUser> userManager)
        {
            var user = await userManager.FindByEmailAsync(configuration.GetSection(WebConstants.UserSettingsString)[WebConstants.UserEmailString]);

            if (user == null)
            {
                var powerUser = new KeepFitUser
                {
                    UserName = configuration.GetSection(WebConstants.UserSettingsString)[WebConstants.UsernameString],
                    Email    = configuration.GetSection(WebConstants.UserSettingsString)[WebConstants.UserEmailString],
                    FullName = configuration.GetSection(WebConstants.UserSettingsString)[WebConstants.FullNameString],
                    Basket   = new Basket()
                };

                string userPassword = configuration.GetSection(WebConstants.UserSettingsString)[WebConstants.UserPasswordString];

                powerUser.EmailConfirmed = true;

                var createPowerUser = await userManager.CreateAsync(powerUser, userPassword);

                if (createPowerUser.Succeeded)
                {
                    //here we tie the new user to the "Administrator" role
                    await userManager.AddToRoleAsync(powerUser, WebConstants.AdministratorRoleName);
                }
            }

            await this.next(context);
        }
        private void SeedUser()
        {
            var user = new KeepFitUser()
            {
                Id       = UserOneId,
                UserName = UserOneName,
            };

            this.context.Users.Add(user);
            this.context.SaveChanges();
        }
Esempio n. 4
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new KeepFitUser
                {
                    UserName = Input.Email,
                    Email    = Input.Email,
                    Basket   = new Basket()
                };

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

                if (userEmailAlreadyExists != null)
                {
                    ModelState.AddModelError("Input.Email", EmailAlreadyExistMessage);

                    return(this.Page());
                }

                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    await _userManager.AddToRoleAsync(user, WebConstants.UserRoleName);

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    //Comment the line below to prevent automatic login when user register! Need to confirm email first
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
        private void SeedUsers()
        {
            for (int i = 0; i < LoopIterations; i++)
            {
                var user = new KeepFitUser()
                {
                    Id       = i == 0 ? UserOneId : UserOneId + i,
                    UserName = i == 0 ? UserOneName : UserOneName + i,
                };

                this.context.Users.Add(user);
            }

            this.context.SaveChanges();
        }
Esempio n. 6
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new KeepFitUser
                {
                    UserName = Input.Email,
                    Email    = Input.Email,
                    Basket   = new Basket()
                };

                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
        private void SeedUsers()
        {
            for (int i = 0; i < LoopIterations; i++)
            {
                var user = new KeepFitUser()
                {
                    Id          = i == 0 ? UserOneId : UserOneId + i,
                    UserName    = i == 0 ? UserOneName : UserOneName + i,
                    PhoneNumber = i == 0 ? Phone : Phone + i,
                    FullName    = i == 0 ? FullName : FullName + i,
                    Basket      = new Basket()
                    {
                        Id = i == 0 ? BasketId : BasketId + i
                    }
                };

                this.context.Users.Add(user);
            }

            this.context.SaveChanges();
        }