Example #1
0
        public int ForgotPasswordSendNewPassword(string email)
        {
            if (string.IsNullOrEmpty(email))
            {
                return(1);
            }

            if (!IsValidEmail(email))
            {
                return(2);
            }

            var existingUserByEmail = userRepository.GetByEmail(email);

            if (existingUserByEmail == null)
            {
                return(3);
            }

            var newPassword = GenerateNewPassword();

            (byte[] passwordHash, byte[] passwordSalt) = GetPasswordHashAndSalt(newPassword);
            existingUserByEmail.PasswordSalt           = passwordSalt;
            existingUserByEmail.PasswordHash           = passwordHash;

            userRepository.Update(existingUserByEmail);

            MailController mail = new MailController(configuration);

            mail.SendEmailForgotPassword(email, newPassword);

            return(0);
        }
Example #2
0
        public int Register(string username, string password, string email)
        {
            var existingUserByEmail = userRepository.GetByEmail(email);
            var existingUserByName  = userRepository.GetByName(username);

            if (existingUserByName != null)
            {
                return(1);
            }

            if (existingUserByEmail != null)
            {
                return(2);
            }

            if (password == null || password.Length < 5 || !password.Any(character => char.IsDigit(character)))
            {
                return(3);
            }

            if (username == null || !username.Any(character => char.IsLetter(character)))
            {
                return(4);
            }

            if (email == null)
            {
                return(5);
            }

            if (!IsValidEmail(email))
            {
                return(6);
            }

            (byte[] passwordHash, byte[] passwordSalt) = GetPasswordHashAndSalt(password);

            Guid g = Guid.NewGuid();

            var user = new User
            {
                Name         = username,
                PasswordHash = passwordHash,
                IsActive     = false,
                PasswordSalt = passwordSalt,
                guid         = g,
                Email        = email,
                RegisterAt   = DateTime.UtcNow
            };

            userRepository.Add(user);
            var registeredUserId = userRepository.GetByName(user.Name).Id;

            MailController mail = new MailController(configuration);

            mail.SendEmailRegistration(g, username, email);

            return(0);
        }