Exemplo n.º 1
0
        public async Task <IActionResult> recoveryConfirmation(recoveryConfirmationDto _recoveryConfirmationDto)
        {
            _recoveryConfirmationDto.RECOVERY_EMAIL = _recoveryConfirmationDto.RECOVERY_EMAIL.ToLower();

            bool userExists = await _authRepo.UserExists(_recoveryConfirmationDto.RECOVERY_EMAIL);

            if (!userExists)
            {
                return(BadRequest("account does not exist"));
            }

            Token tokenService = new Token(_config.GetSection("AppSettings:SecretKey").Value);
            bool  isTokenValid = tokenService.ValidateRecoveryToken(_recoveryConfirmationDto.RECOVERY_TOKEN, _recoveryConfirmationDto.RECOVERY_EMAIL);

            if (!isTokenValid)
            {
                return(BadRequest("recovery session is not valid"));
            }

            UserLog User = await _accountRepo.GetUserFromEmail(_recoveryConfirmationDto.RECOVERY_EMAIL);

            Confirmationcode currentConfirmationCode = await _accountRepo.ConfirmRecoveryCode(_recoveryConfirmationDto.RECOVERY_CODE, User.IduserLog);

            if (currentConfirmationCode == null || currentConfirmationCode.ExpiryTime < DateTime.UtcNow)
            {
                return(BadRequest("code is either expired or invalid"));
            }

            JwtSecurityToken reset_token = tokenService.GenerateResetToken(_recoveryConfirmationDto.RECOVERY_EMAIL);

            return(Ok(new {
                reset_token = new JwtSecurityTokenHandler().WriteToken(reset_token)
            }));
        }
        public async Task <bool> StoreActivationCode(Confirmationcode _confirmationCode)
        {
            await _context.Confirmationcode.AddAsync(_confirmationCode);

            await _context.SaveChangesAsync();

            return(true);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> recover(accountForRecoveryDto _accountForRecoveryDto)
        {
            _accountForRecoveryDto.RECOVERY_EMAIL = _accountForRecoveryDto.RECOVERY_EMAIL.ToLower();


            UserLog User = await _accountRepo.GetUserFromEmail(_accountForRecoveryDto.RECOVERY_EMAIL);

            if (User == null)
            {
                return(BadRequest("Unable to find any account associated with this email"));
            }


            JwtSecurityToken recoveryToken = new JwtSecurityToken();

            recoveryToken = new Token(_config.GetSection("AppSettings:SecretKey").Value)
                            .GenerateRecoveryToken(_accountForRecoveryDto.RECOVERY_EMAIL);

            CodeGenerator codeGenerator = new CodeGenerator();
            string        recoveryCode  = codeGenerator.RecoveryCodeGenerator();

            var codeToSave = new Confirmationcode()
            {
                ConfirmationCode1 = recoveryCode,
                ConfirmationType  = "RECOVERY_CODE",
                GeneratedOn       = DateTime.UtcNow,
                ExpiryTime        = DateTime.UtcNow.AddDays(1),
                Used   = "F",
                UserId = User.IduserLog
            };

            _accountRepo.StoreCode(codeToSave);

            Mailer mailer = new Mailer(_config.GetSection("AppSettings:MailerEmail").Value, _config.GetSection("AppSettings:MailerPassword").Value);

            mailer.sendRecoveryMail(_accountForRecoveryDto.RECOVERY_EMAIL, new JwtSecurityTokenHandler().WriteToken(recoveryToken), recoveryCode);


            return(Ok(new
            {
                recovery_token = new JwtSecurityTokenHandler().WriteToken(recoveryToken)
            }));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> sendActivationMail(mailForActivationDto _mailForActivationDto)
        {
            _mailForActivationDto.USER_EMAIL = _mailForActivationDto.USER_EMAIL.ToLower();

            UserLog User = await _accountRepo.GetUserFromEmail(_mailForActivationDto.USER_EMAIL);

            if (User == null || User.IduserLog != _mailForActivationDto.USER_ID || User.Activated != "F")
            {
                return(BadRequest());
            }

            CodeGenerator codeGenerator  = new CodeGenerator();
            String        activationCode = codeGenerator.ActivationCodeGenerator();

            Confirmationcode _confirmationCode = new Confirmationcode();

            _confirmationCode.ConfirmationCode1 = activationCode;

            _confirmationCode.ConfirmationType = "ACTIVATION_CODE";
            _confirmationCode.GeneratedOn      = DateTime.UtcNow;
            _confirmationCode.ExpiryTime       = DateTime.UtcNow.AddDays(1);
            _confirmationCode.Used             = "F";
            _confirmationCode.UserId           = _mailForActivationDto.USER_ID;



            await _accountRepo.StoreActivationCode(_confirmationCode);


            Token            tokenGenerator  = new Token(_config.GetSection("AppSettings:SecretKey").Value);
            JwtSecurityToken activationToken = tokenGenerator.GenerateActivationToken(_mailForActivationDto.USER_ID);


            // sending activation mail
            Mailer currentMailer = new Mailer(_config.GetSection("AppSettings:MailerEmail").Value, _config.GetSection("AppSettings:MailerPassword").Value);

            currentMailer.sendActivationMail(_mailForActivationDto.USER_EMAIL, new JwtSecurityTokenHandler().WriteToken(activationToken), activationCode, _mailForActivationDto.USER_ID);

            return(Ok(new {
                activation_token = new JwtSecurityTokenHandler().WriteToken(activationToken)
            }));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> activateUserAccount(userForActivationDto _userForActivationDto)
        {
            UserLog User = await _accountRepo.GetUserFromUserID(_userForActivationDto.USER_ID);

            if (User == null)
            {
                return(BadRequest("something went wrong"));
            }

            Token tokenService = new Token(_config.GetSection("AppSettings:SecretKey").Value);

            bool isActivationTokenValid = tokenService.ValidateActivationToken(_userForActivationDto.ACTIVATION_TOKEN, _userForActivationDto.USER_ID);

            if (!isActivationTokenValid)
            {
                return(BadRequest("activation session is either invalid or expired"));
            }

            Confirmationcode activationConfirmationCode = await _accountRepo.GetActivationCode(_userForActivationDto.ACTIVATION_CODE, _userForActivationDto.USER_ID);

            if (activationConfirmationCode == null || activationConfirmationCode.ExpiryTime < DateTime.UtcNow)
            {
                return(BadRequest("code is either invalid or expired"));
            }

            User.Activated = "T";

            bool isCodeRemoved = _accountRepo.RemoveConfirmationCode(activationConfirmationCode);

            if (!isCodeRemoved)
            {
                return(BadRequest("Something went wrong"));
            }

            await _accountRepo.SaveUserDetails();

            return(Ok());
        }
        public async void StoreCode(Confirmationcode confirmationcode)
        {
            await _context.Confirmationcode.AddAsync(confirmationcode);

            await _context.SaveChangesAsync();
        }
        public async Task <Confirmationcode> ConfirmRecoveryCode(string code, int userID)
        {
            Confirmationcode confirmationCode = await _context.Confirmationcode.FirstOrDefaultAsync(i => i.ConfirmationCode1 == code && i.ConfirmationType == "RECOVERY_CODE" && userID == i.UserId);

            return(confirmationCode);
        }
        public async Task <Confirmationcode> GetActivationCode(string code, int userID)
        {
            Confirmationcode confirmationCode = await _context.Confirmationcode.FirstOrDefaultAsync(i => i.UserId == userID && i.ConfirmationCode1 == code && i.ConfirmationType == "ACTIVATION_CODE");

            return(confirmationCode);
        }
 public bool RemoveConfirmationCode(Confirmationcode _confirmationCode)
 {
     _context.Confirmationcode.Remove(_confirmationCode);
     return(true);
 }