public string ResetPswd(int selecteduser) { var user = _context.UserLoginInformations.Single(x => x.USERID.Equals(selecteduser)); var info = new KPTY_USER_FORGOT_PASS_OTP_REQ_TBL(); var intialLogin = new UserRegitrationForInitialLogin(); Session["OTPCount"] = 0; var otp = GenerateOTP(4); try { var loggedinUser = LoginController.ActiveUser; info.USERID = user.USERID; info.ModifiedById = loggedinUser.USERID.ToString(); info.CreatedById = loggedinUser.USERID.ToString(); info.CREATED_DATE = DateTime.Now; info.ModifiedDate = DateTime.Now; info.STATUS = 1; info.OTPVAL = otp; try { using (var transactionScope = new TransactionScope()) { try { using (var db = new kryptoEntities1()) // Context object { db.Database.ExecuteSqlCommand( "delete from KPTY_USER_FORGOT_PASS_OTP_REQ_TBL where UserId = {0}", selecteduser); db.KPTY_USER_FORGOT_PASS_OTP_REQ_TBL.Add(info); db.UserRegitrationForInitialLogins.Remove( db.UserRegitrationForInitialLogins.SingleOrDefault(u => u.USERID == selecteduser)); intialLogin.IsInitialLogin = true; intialLogin.IsTermsAccepted = false; intialLogin.IsSecQuestEnabled = false; intialLogin.IsPasswordUpdated = false; intialLogin.IsMpinCreated = false; intialLogin.Notes = null; intialLogin.Status = 1; intialLogin.ModifiedById = loggedinUser.USERID.ToString(); intialLogin.ModifiedDate = DateTime.Now; intialLogin.USERID = selecteduser; intialLogin.Createdate = DateTime.Now; intialLogin.CreatedById = loggedinUser.USERID.ToString(); db.UserRegitrationForInitialLogins.Add(intialLogin); db.SaveChanges(); db.Database.ExecuteSqlCommand("delete from KPTY_USER_SECQURITY_QUEST_ANS where UserId = {0}", selecteduser); Encryption eny = new Encryption(); string EncrptPassword = eny.EncryptString(otp.ToString()); user.Password = EncrptPassword; user.ModifiedById = loggedinUser.USERID.ToString(); user.ModifiedDate = DateTime.Now; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } transactionScope.Complete(); // transaction complete var recemail = user.EmailId; if (!SendOTPMail(otp, recemail, user.FirstName)) { return("Invalid Email"); } } catch (Exception ee) { return("FAIL"); } } } catch (Exception exception) { return("FAIL"); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } return("FAIL"); } return("SUCESS"); }
public string ResetPswd(int selecteduser) { UserLoginInformation user = _context.UserLoginInformations.SingleOrDefault(x => x.USERID.Equals(selecteduser)); KPTY_USER_FORGOT_PASS_OTP_REQ_TBL info = new KPTY_USER_FORGOT_PASS_OTP_REQ_TBL(); UserRegitrationForInitialLogin IntialLogin = new UserRegitrationForInitialLogin(); Session["OTPCount"] = 0; var OTP = GenerateOTP(4); try { UserLoginInformation loggedinUser = (UserLoginInformation)LoginController.ActiveUser; //ResetOTPStatus(loggedinUser.USERID); info.USERID = user.USERID; info.ModifiedById = loggedinUser.USERID.ToString(); info.CreatedById = loggedinUser.USERID.ToString(); info.CREATED_DATE = DateTime.Now; info.ModifiedDate = DateTime.Now; info.STATUS = 1; info.OTPVAL = OTP; try { using (TransactionScope transactionScope = new TransactionScope()) { try { using (kryptoEntities1 db = new kryptoEntities1()) // Context object { db.Database.ExecuteSqlCommand("delete from KPTY_USER_FORGOT_PASS_OTP_REQ_TBL where UserId = {0}", selecteduser); db.KPTY_USER_FORGOT_PASS_OTP_REQ_TBL.Add(info); db.SaveChanges(); db.Database.ExecuteSqlCommand("delete from UserRegitrationForInitialLogin where UserId = {0}", selecteduser); IntialLogin.IsInitialLogin = true; IntialLogin.IsTermsAccepted = false; IntialLogin.IsSecQuestEnabled = false; IntialLogin.IsPasswordUpdated = false; IntialLogin.IsMpinCreated = false; IntialLogin.Notes = null; IntialLogin.Status = 1; IntialLogin.ModifiedById = loggedinUser.USERID.ToString(); IntialLogin.ModifiedDate = DateTime.Now; IntialLogin.USERID = selecteduser; IntialLogin.Createdate = DateTime.Now; IntialLogin.CreatedById = loggedinUser.USERID.ToString(); db.UserRegitrationForInitialLogins.Add(IntialLogin); db.SaveChanges(); } transactionScope.Complete(); // transaction complete var recemail = user.EmailId; var msg = "Dear User,\n\n Your request to process the reset password is successful and your new OTP generated is " + OTP + " . Please use it to login again. \n This is system generated message please do not reply."; bool x1 = SendEmail("Your Password has been reset", msg, recemail, user.FirstName); if (!x1) { return("Invalid Email"); } } catch (Exception ee) { return("FAIL"); } } } catch (Exception exception) { return("FAIL"); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } return("FAIL"); } return("SUCESS"); }