Exemple #1
0
        public bool ConfirmEmail(ConfirmationKey key)
        {
            var user = GetUserById(key.UserId);

            if (user == null)
            {
                return(false);
            }

            var tmpUser = user;

            tmpUser.EmailConfirmed = true;

            if (!UpdateUser(tmpUser))
            {
                return(false);
            }

            key.Revoked = true;

            if (!RevokeConfirmationKey(key))
            {
                return(false);
            }

            return(true);
        }
        public async Task <OperationResult> Handle(ValidatePwdTokenCommand request, CancellationToken cancellationToken)
        {
            OperationResult Result       = new OperationResult();
            ConfirmationKey recoveryInfo = await _unitOfWork.ConfirmationKeys.FirstOrDefaultAsync(s => s.Id == request.RecoveryKey);

            // Check recovery info exists
            if (recoveryInfo == null)
            {
                Result.ErrorContent = new ErrorContent("The provided token does not exist", ErrorOrigin.Client);
                return(Result);
            }

            if (recoveryInfo.Status != RequestStatus.InProgress)
            {
                Result.ErrorContent = new ErrorContent("The provided token has already been used or expired.", ErrorOrigin.Client);
                return(Result);
            }

            // Update the user's password
            User user = await _unitOfWork.Users.FirstOrDefaultAsync(s => s.Id == recoveryInfo.UserId);

            if (user == null)
            {
                Result.ErrorContent = new ErrorContent("No user found with the provided id.", ErrorOrigin.Client);
                return(Result);
            }
            user.Password     = Helpers.HashPassword(request.NewPassword);
            user.LastModified = DateTime.UtcNow;

            // Update the recovery status
            recoveryInfo.Status = RequestStatus.Completed;

            // Save changes
            return(await _unitOfWork.CompleteAsync(Result));
        }
Exemple #3
0
        public bool ConfirmEmail(ConfirmationKey key)
        {
            if (key == null)
            {
                return(false);
            }

            return(_userRepository.ConfirmEmail(key));
        }
Exemple #4
0
 public bool DeleteConfirmationKey(ConfirmationKey key)
 {
     try
     {
         _userRepository.DeleteConfirmationKey(key);
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Exemple #5
0
 public bool DeleteConfirmationKey(ConfirmationKey key)
 {
     try
     {
         _context.ConfirmationKeys.Remove(key);
         _context.SaveChanges();
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Exemple #6
0
 public bool RevokeConfirmationKey(ConfirmationKey key)
 {
     try
     {
         _context.ConfirmationKeys.Update(key);
         _context.SaveChanges();
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Exemple #7
0
 public bool AddConfirmationKey(ConfirmationKey key)
 {
     try
     {
         _context.ConfirmationKeys.Add(key);
         _context.SaveChanges();
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Exemple #8
0
        public void ConfirmEmail(ConfirmationKey key)
        {
            var user = GetUserById(key.UserId);

            var tmpUser = user;

            tmpUser.EmailConfirmed = true;

            UpdateUser(tmpUser);

            key.Revoked = true;

            RevokeConfirmationKey(key);
        }
        public async Task <OperationResult> Handle(RequestConfirmationEmailCommand request, CancellationToken cancellationToken)
        {
            string          userId = _caller.GetUserId();
            OperationResult Result = new OperationResult();
            User            user   = await _unitOfWork.Users.FirstOrDefaultAsync(s => s.Id == userId);

            // Check user exist
            if (user == null)
            {
                Result.ErrorContent = new ErrorContent("No user found with the provided email.", ErrorOrigin.Client);
                return(Result);
            }

            // Check email service is up
            HealthCheckResult health = await _mediatr.Send(new EmailHealthCheckQuery());

            if (health.Status != HealthStatus.Healthy)
            {
                Result.ErrorContent = new ErrorContent("Internal email server error, please check again later.", ErrorOrigin.Server);
                return(Result);
            }
            // Check email confirmation status
            if (user.EmailConfirmed)
            {
                Result.ErrorContent = new ErrorContent("The email has been already confirmed.", ErrorOrigin.Client);
                return(Result);
            }
            // Create and store a mail confirmation token
            ConfirmationKey token = new ConfirmationKey()
            {
                Id         = Helpers.GenerateUniqueId(),
                GenerateAt = DateTime.UtcNow,
                Status     = RequestStatus.InProgress,
                UserId     = userId,
                IpAdress   = request.UserIp
            };
            await _unitOfWork.ConfirmationKeys.AddAsync(token);

            // Save changes to db
            Result = await _unitOfWork.CompleteAsync(Result);

            if (Result.State == OperationState.Success)
            {
                // Send confirmation email
                await _mediatr.Send(new SendConfirmationEmailCommand(user, token));
            }

            return(Result);
        }
Exemple #10
0
        public ConfirmationKey GenerateConfirmationKey(User user)
        {
            var key = Guid.NewGuid().ToString().Replace("-", "");

            var confirmationKey = new ConfirmationKey
            {
                UserId  = user.Id,
                Key     = key,
                Revoked = false
            };

            _userRepository.AddConfirmationKey(confirmationKey);

            return(confirmationKey);
        }
Exemple #11
0
        public bool ConfirmEmail(ConfirmationKey key)
        {
            try
            {
                if (key == null)
                {
                    return(false);
                }

                _userRepository.ConfirmEmail(key);
            }
            catch (Exception e)
            {
                return(false);
            }
            return(true);
        }
Exemple #12
0
        private EmailMessage GetResetPasswordTpl(ConfirmationKey token, User user)
        {
            string templateName = "ResetPassword.html";
            string templatePath = GetTemplatePath(templateName);
            string recoveryLink = BaseUrl + "/recoverpassword/" + token.Id;

            using StreamReader SourceReader = File.OpenText(templatePath);
            BodyBuilder builder = new BodyBuilder()
            {
                HtmlBody = SourceReader.ReadToEnd()
            };
            EmailMessage message = new EmailMessage
            {
                Subject     = "Reset Your Email",
                HtmlContent = string.Format(builder.HtmlBody, user.UserName, _emailSetting.Sender.Name, _emailSetting.Sender.Support, recoveryLink)
            };

            return(message);
        }
        public async Task <OperationResult> Handle(ResetPasswordCommand request, CancellationToken cancellationToken)
        {
            OperationResult Result = new OperationResult();
            User            user   = await _unitOfWork.Users.FirstOrDefaultAsync(s => s.Email == request.Email);

            // Check the user exist
            if (user == null)
            {
                Result.ErrorContent = new ErrorContent("No user found with the provided email.", ErrorOrigin.Client);
                return(Result);
            }
            // Check email service is up
            HealthCheckResult health = await _mediatr.Send(new EmailHealthCheckQuery());

            if (health.Status != HealthStatus.Healthy)
            {
                Result.ErrorContent = new ErrorContent("Internal email server error, please check again later.", ErrorOrigin.Server);
                return(Result);
            }
            // Generate a password reset candidate
            ConfirmationKey token = new ConfirmationKey()
            {
                Id         = Helpers.GenerateUniqueId(),
                Status     = RequestStatus.InProgress,
                GenerateAt = DateTime.Now,
                UserId     = user.Id,
                IpAdress   = request.ClientIp
            };

            // add the key to current collection
            _unitOfWork.ConfirmationKeys.Add(token);

            // Save to db
            Result = await _unitOfWork.CompleteAsync(Result);

            if (Result.State == OperationState.Success)
            {
                // if success, send an email to the user
                await _mediatr.Send(new SendPassRecoveryCommand(user, token));
            }

            return(Result);
        }
        private EmailMessage GetConfirmEmailTpl(ConfirmationKey emailKey, User user)
        {
            string templateName          = "ConfirmEmail.html";
            string templatePath          = GetTemplatePath(templateName);
            string emailConfirmationLink = BaseUrl + "/confirmemail/" + emailKey.Id;

            using StreamReader SourceReader = File.OpenText(templatePath);
            BodyBuilder builder = new BodyBuilder()
            {
                HtmlBody = SourceReader.ReadToEnd()
            };
            EmailMessage message = new EmailMessage
            {
                Subject     = "Confirm Your Email",
                HtmlContent = string.Format(builder.HtmlBody, user.UserName, _emailSetting.Sender.Name, _emailSetting.Sender.Support, emailConfirmationLink)
            };

            return(message);
        }
Exemple #15
0
        public async Task <OperationResult> Handle(CheckPwdRecoveryInfoQuery request, CancellationToken cancellationToken)
        {
            OperationResult Result       = new OperationResult();
            ConfirmationKey recoveryInfo = await _unitOfWork.ConfirmationKeys.FirstOrDefaultAsync(s => s.Id == request.RecoeryId);

            // Check recovery info exists
            if (recoveryInfo == null)
            {
                Result.ErrorContent = new ErrorContent("The provided token does not exist", ErrorOrigin.Client);
                return(Result);
            }

            // Generated link expires after 24h (by a background alien thread)
            if (DateTime.UtcNow > recoveryInfo.GenerateAt.AddDays(1))
            {
                Result.ErrorContent = new ErrorContent("The provided token was expired", ErrorOrigin.Client);
            }
            if (recoveryInfo.Status != RequestStatus.InProgress)
            {
                Result.ErrorContent = new ErrorContent("The provided token has already been used or expired", ErrorOrigin.Client);
            }

            return(Result);
        }
Exemple #16
0
 public void RevokeConfirmationKey(ConfirmationKey key)
 {
     _context.ConfirmationKeys.Update(key);
     _context.SaveChanges();
 }
Exemple #17
0
 public bool DeleteConfirmationKey(ConfirmationKey key)
 {
     return(_userRepository.DeleteConfirmationKey(key));
 }
Exemple #18
0
 public bool RevokeConfirmationKey(ConfirmationKey key)
 {
     return(_userRepository.RevokeConfirmationKey(key));
 }
Exemple #19
0
 public SendPassRecoveryCommand(User to, ConfirmationKey tokenKey)
 {
     To       = to;
     TokenKey = tokenKey;
 }
Exemple #20
0
 public void AddConfirmationKey(ConfirmationKey key)
 {
     _context.ConfirmationKeys.Add(key);
     _context.SaveChanges();
 }
Exemple #21
0
 public void DeleteConfirmationKey(ConfirmationKey key)
 {
     _context.ConfirmationKeys.Remove(key);
     _context.SaveChanges();
 }
 public SendConfirmationEmailCommand(User to, ConfirmationKey tokenKey)
 {
     To       = to;
     TokenKey = tokenKey;
 }