public async Task <IActionResult> ForgetPassword(ForgetPassword model)
        {
            if (ModelState.IsValid)
            {
                CommonUser user = await _commonUserSvc.CheckUser(model.Email);

                if (user != null)
                {
                    string token       = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                    var    callbackUrl = Url.Action("ResetPassword", "User", new { code = token });

                    using (var tx = CommonContext.Database.BeginTransaction())
                    {
                        user.Token    = token;
                        user.TokenUtc = DateTime.UtcNow;
                        CommonContext.Users.Update(user);

                        await CommonContext.SaveChangesAsync();

                        tx.Commit();
                    }

                    _logger.Error($"Sending Email email:{model.Email} callback:{callbackUrl}");
                    bool result = await _mailSvc.SendPasswordResetEmail(model.Email, callbackUrl);

                    _logger.Error($"Email Sent:");
                }
                ViewBag.SucessMessage = "Please check your email for a link to reset your password";
                return(View());
            }

            return(View(model));
        }
Пример #2
0
        public async Task <IActionResult> ResetPassword([FromBody] ResetPasswordModel model)
        {
            var response = new APIResponse <ResetPasswordModel>()
            {
                Success = true
            };

            try
            {
                if (ModelState.IsValid)
                {
                    CommonUser user = await _commonUserSvc.CheckUser(model.Email);

                    if (user != null)
                    {
                        string token       = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                        var    callbackUrl = Url.Action("ResetPassword", "User", new { code = token });

                        using (var tx = CommonContext.Database.BeginTransaction())
                        {
                            user.Token    = token;
                            user.TokenUtc = DateTime.UtcNow;
                            CommonContext.Users.Update(user);

                            await CommonContext.SaveChangesAsync();

                            tx.Commit();
                        }

                        bool result = await _mailSvc.SendPasswordResetEmail(model.Email, callbackUrl);

                        response.Message = "Please check your email for a link to reset your password";
                    }
                    else
                    {
                        response.Message = "Please check your email for a link to reset your password";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Errors.AddRange(ModelState.ToErrors());
                }

                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                return(Ok(response));
            }
        }