コード例 #1
0
        public ResultObj Reset(string email)
        {
            try
            {
                using (var db = new AppDbContext())
                {
                    var existing = db.Users.FirstOrDefault(x => x.UserName == email && !x.Hidden && !x.Locked);
                    if (existing == null)
                    {
                        throw new Exception("Sorry email is not valid. Enjoy!!");
                    }
                    var newRecord = new ResetRequest
                    {
                        Email    = email,
                        Token    = StringHelpers.GenerateRandomString(32),
                        Date     = DateTime.Now,
                        Ip       = Request.Headers.Referrer.AbsoluteUri,
                        IsActive = true
                    };
                    db.ResetRequests.Add(newRecord);

                    // create a password reset entry
                    var link     = Request.Headers.Referrer.AbsoluteUri + "#/resetpassword/" + newRecord.Token;
                    var emailMsg = new EmailOutboxEntry
                    {
                        Message =
                            $"<h3>Password Reset Request</h3> <br/><br/> Please follow the link below to change your password. <br/><br/><b><a href='{link}'>Click here</a></b> to reset your password.<br/><br/><br/><br/>Please ignore this message if you did not make this request.<br/><br/>Thank you. <br/>",
                        Subject  = "Password Reset",
                        Sender   = "*****@*****.**",
                        Receiver = newRecord.Email,
                        Created  = DateTime.Now
                    };
                    db.EmailOutboxEntries.Add(emailMsg);
                    db.SaveChanges();
                    MessageHelpers.SendEmailMessage(emailMsg.Id);

                    return(WebHelpers.BuildResponse(null, "Password reset link has been sent to your email.", true, 1));
                }
            }
            catch (Exception e)
            {
                return(WebHelpers.ProcessException(e));
            }
        }