Esempio n. 1
0
        /// <summary>
        /// Регистрация.
        /// </summary>
        public async Task <Guid> SignUpAsync(SignUpViewModel model)
        {
            if (await _userRepository.GetByEmailAsync(model.Email) != null)
            {
                throw new Exception($"Username '{model.Email}' is already in use.");
            }

            try
            {
                var user = _mapper.Map <User>(model);
                user.Password    = _passwordHashService.GetHash(user.Password);
                user.ConfirmCode = Guid.NewGuid().ToString("N");

                var userId = await _userRepository.InsertAsync(user);

                var confirmUrl = $"/api/account/confirm/{user.ConfirmCode}";
                await _notificationService.SendAccountConfirmationAsync(user.Email, confirmUrl);

                return(userId);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Esempio n. 2
0
        public ActionResult Register(RegisterDto registerDto)
        {
            if (!ModelState.IsValid)
            {
                return(View(registerDto));
            }

            if (registerDto.RepeatedPassword != registerDto.Password)
            {
                ModelState.AddModelError(nameof(registerDto.Password), "Пароли не совпадают");
                ModelState.AddModelError(nameof(registerDto.RepeatedPassword), "Пароли не совпадают");
                return(View(registerDto));
            }

            var existedUser = UserDataStore.GetAll()
                              .SingleOrDefault(user => user.Login == registerDto.Login && user.PasswordHash == string.Empty);

            if (existedUser == null)
            {
                ModelState.AddModelError(nameof(registerDto.Login),
                                         "Данный логин недоступен, обратитесь к администратору");
                return(View(registerDto));
            }

            existedUser.PasswordHash = PasswordHashService.GetHash(registerDto.Password);
            UserDataStore.Update(existedUser);

            return(RedirectToAction("Index"));
        }
Esempio n. 3
0
        public User Login(LoginPassDto loginPasswordDto)
        {
            var passHash = PasswordHashService.GetHash(loginPasswordDto.Password);
            var user     = UserDataStore.GetAll()
                           .SingleOrDefault(u => u.Login == loginPasswordDto.Login && u.PasswordHash == passHash);

            if (user != null)
            {
                CreateCookie(user, loginPasswordDto.RememberMe);
            }

            return(user);
        }