public object checkOTP(string userOTP, string email) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { //receive Otp from db User user = db.Users.Where(z => z.UserEmail == email).FirstOrDefault(); if (user != null) { One_Time_Pin otp = db.One_Time_Pin.Where(z => z.userID == user.UserID && z.OTP == userOTP).FirstOrDefault(); if (otp != null) { if (otp.ExpiryTime >= DateTime.Now) { toReturn.Message = "One Time Pin successfully verified"; } else { toReturn.Message = "One Time Pin has expired"; } } else { toReturn.Error = "One time pin is incorrect"; } } else { toReturn.Error = "User not found"; } } catch (Exception error) { toReturn.Error = "Something went wrong:" + error; } return(toReturn); }
public object sendEmail(string email) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { User user = db.Users.Where(z => z.UserEmail == email).FirstOrDefault(); if (user != null) { //generating a reset password one time pin Random rnd = new Random(); string OTP = (rnd.Next(100000, 999999)).ToString(); //getting the time of generation of the OTP DateTime genTime = DateTime.Now; //getting the expiry time of the OTP DateTime expiryTime = genTime.AddHours(3); //Saving otp details in the db One_Time_Pin otpObj = new One_Time_Pin(); otpObj.OTP = OTP; otpObj.ExpiryTime = expiryTime; otpObj.GenerationTime = genTime; otpObj.userID = user.UserID; db.One_Time_Pin.Add(otpObj); db.SaveChanges(); //sending an email using (MailMessage mail = new MailMessage()) { mail.From = new MailAddress("*****@*****.**"); mail.To.Add(email); mail.Subject = "Reset Password One Time Pin"; mail.Body = "<h1>Your one time pin to reset your password is: </h1>" + OTP + "<h1>The one time pin will expire in 3 hours. <h1>"; mail.IsBodyHtml = true; using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587)) { smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "Ordra@444"); smtp.EnableSsl = true; smtp.Send(mail); toReturn.Message = "Mail sent"; } } } else { toReturn.Error = "User email not found"; } return(toReturn); } catch { toReturn.Error = "Mail unsuccessfully sent"; } return(toReturn); }