Esempio n. 1
0
        public async Task <IActionResult> SendPasswordResetLinkMobile([FromBody] ForgotPasswordReq model)
        {
            try
            {
                var user = await _userManager.FindByEmailAsync(model.Email);

                if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
                {
                    return(Ok(false));
                }

                var code = await _userManager.GeneratePasswordResetTokenAsync(user);

                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));

                bool available = true;
                int  mCode     = 0;

                Random rnd = new Random();
                do
                {
                    mCode = rnd.Next(100000, 999999);
                    var isAvailable = _context.passwordResetTokens.Where(o => o.MobileCode == mCode.ToString()).FirstOrDefault();
                    available = isAvailable == null;
                } while (!available);


                //insert to database
                PasswordResetToken passwordResetToken = new PasswordResetToken
                {
                    UserID       = user.Id,
                    IsActive     = true,
                    RegistedDate = DateTime.Now,
                    MobileCode   = mCode.ToString(),
                    Token        = code
                };

                _context.passwordResetTokens.Add(passwordResetToken);
                _ = _context.SaveChangesAsync();

                ForgotEmailDataMobile forgotEmailData = new ForgotEmailDataMobile
                {
                    Company  = _config.Value.CompanyName,
                    Email    = model.Email,
                    code     = mCode.ToString(),
                    SiteName = _config.Value.SolutionName,
                    SiteUrl  = _config.Value.BaseURL
                };

                await _emailSender.SendEmailAsync(model.Email, "APlus Account Password Reset", DataFormatManager.GetFormatedForgotPasswordEmailTemplate(forgotEmailData, _hostingEnvironment.ContentRootPath + _templateParams.Value.ForgotPasswordMailTemplateMobile));

                return(Ok(true));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 2
0
        // TODO: Apply throttling
        public async Task <IHttpActionResult> ForgotPassword(ForgotPasswordReq forgotPasswordDto)
        {
            var user = AppUserManager.FindByEmail(forgotPasswordDto.Email);

            if (user == null || !(AppUserManager.IsEmailConfirmed(user.Id)))
            {
                // Don't reveal that the user does not exist or is not confirmed
                return(BadRequest());
            }

            var message = await SendForgotPasswordEmail(user);

            var response = new BaseResponseDto();

            response.Message = message;
            return(Ok(response));
        }
Esempio n. 3
0
        public async Task <IActionResult> SendPasswordResetLink([FromBody] ForgotPasswordReq model)
        {
            try
            {
                var user = await _userManager.FindByEmailAsync(model.Email);

                if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
                {
                    return(NotFound("Email address is not registered or wrong email."));
                }

                var code = await _userManager.GeneratePasswordResetTokenAsync(user);

                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));


                //insert to database
                PasswordResetToken passwordResetToken = new PasswordResetToken
                {
                    UserID       = user.Id,
                    IsActive     = true,
                    RegistedDate = DateTime.Now,
                    Token        = code
                };

                _context.passwordResetTokens.Add(passwordResetToken);
                _ = _context.SaveChangesAsync();

                ForgotEmailData forgotEmailData = new ForgotEmailData
                {
                    Company          = _config.Value.CompanyName,
                    Email            = model.Email,
                    PasswordResetUrl = _config.Value.ResetEmailUrl + "?token=" + code,
                    SiteName         = _config.Value.SolutionName,
                    SiteUrl          = _config.Value.BaseURL
                };

                await _emailSender.SendEmailAsync(model.Email, "APlus Account Password Reset", DataFormatManager.GetFormatedForgotPasswordEmailTemplate(forgotEmailData, _hostingEnvironment.ContentRootPath + _templateParams.Value.ForgotPasswordMailTemplate));

                return(Ok("Email sent successfully"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> ResetPassword(ForgotPasswordReq model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = _userDbContext.Users.FirstOrDefault(x => x.Email == model.Email);

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

            throw new System.Exception();
            return(Ok());
        }
Esempio n. 5
0
        public async Task <IActionResult> ForgotPassword([FromBody] ForgotPasswordReq req)
        {
            await _mediator.Send(new ForgotPasswordCommand(req.Phone));

            return(Accepted());
        }