public static bool SendVerificationEmail(string UserID) { try { using (var db = new MemberLiteEntities().Init) { var u = db.Users.Select(a => new { a.UserID, a.FirstName, a.OtherNames, a.Email, a.VerificationCode }) .Where(a => a.UserID == UserID) .FirstOrDefault(); if (u == null) { ReturnMessage = "Invalid user"; return(false); } string link = new Uri(string.Format(AppConfig.Url + "access/verifyemail?e={0}&c={1}", u.Email, u.VerificationCode)).AbsoluteUri; string body = File.ReadAllText(AppUtility.AppDataPath + "/MailTemplates/EmailVerification.htm"); body = body.Replace("{site_name}", AppConfig.Name); body = body.Replace("{site_url}", AppConfig.Url); body = body.Replace("{name}", u.OtherNames + " " + u.FirstName); body = body.Replace("{verify_link}", link); body = body.Replace("{email}", u.Email); body = body.Replace("{support_mail}", WebMailer.Support); if (WebMailer.Send(WebMailer.Alert, u.Email, AppConfig.Name + " - Email Verification", body, true)) { ReturnMessage = "Verification message has been sent, please goto your inbox and confirm it now."; return(true); } } } catch (Exception ex) { CustomErrorLogger.Log(ex.Message); } ReturnMessage = "Unable to send verification mail"; return(false); }
public static bool ResetPassword(string Login) { string userID = "", email = "", newPwd, fname = ""; using (var db = new MemberLiteEntities().Init) { long __phone = 0, val = 0; if (Int64.TryParse(Login, out val)) { __phone = Convert.ToInt64(Login); } if (Login.Contains("@")) { if (AppUtility.ValidateEmail(Login)) { var u = db.Users.Select(a => new { a.UserID, a.Email, a.FirstName, a.OtherNames }) .Where(a => a.Email == Login).FirstOrDefault(); if (u != null) { userID = u.UserID; fname = u.OtherNames + " " + u.FirstName; email = Login; } goto notfound; } else { ReturnMessage = "Email address format is incorrect!"; return(false); } } else if (__phone != 0) { var u = db.Users.Select(a => new { a.UserID, a.Email, a.Phone, a.FirstName, a.OtherNames }) .Where(a => a.Phone == __phone).FirstOrDefault(); if (u != null) { userID = u.UserID; fname = u.FirstName; email = Login; } goto notfound; } else { ReturnMessage = "Provide your login email or phone number!"; return(false); } notfound: if (userID == "") { ReturnMessage = "User not found! Please try again."; return(false); } newPwd = AppUtility.GenerateAlphaNumeric(10); string userIDHash = Crypto.SHA256Hash(userID); string pwd = Crypto.SHA256Hash(newPwd.ToUpper()); string finalPwd = Crypto.SHA256Hash(userIDHash + pwd); db.Users.Find(userID).Password = finalPwd; db.SaveChanges(); ReturnMessage = "Password reset ok but could not send email. Pls try again!"; } string msg = File.ReadAllText(AppUtility.AppDataPath + "MailTemplates/PasswordReset.htm"); msg = msg.Replace("{site_name}", AppConfig.Name); msg = msg.Replace("{fullname}", fname); msg = msg.Replace("{new_pwd}", newPwd); msg = msg.Replace("{site_url}", AppConfig.Url); msg = msg.Replace("{support_mail}", WebMailer.Support); if (WebMailer.Send(WebMailer.Alert, email, "Password Reset", msg, true)) { ReturnMessage = "Password reset complete! Check your email for a new password."; } return(true); }