internal void ForgotPassword(ForgotPasswordRequest changePasswordRequest) { logger.Debug("Recived forgotPassword request for encrypted string - " + changePasswordRequest.email); try { changePasswordRequest.email = Decryptor.Decrypt(changePasswordRequest.email).Split('|')[1]; logger.Debug("Recived forgotPassword request for email = " + changePasswordRequest.email); if (!String.IsNullOrEmpty(changePasswordRequest.email)) { user User; using (var ctx = new PetWhizzEntities()) { User = ctx.users.Where(a => a.eMail.ToLower() == changePasswordRequest.email.ToLower()).FirstOrDefault(); } if (User == null) { logger.Error("User not found for given email - " + changePasswordRequest.email); throw new CustomException("User not found for given email", (int)ErrorCode.USERNOTFOUND); } int GeneratedCode = new Random().Next(100000, 999999); var userVerificationInfo = new userVerification() { code = GeneratedCode.ToString(), generatedTime = DateTime.Now, expiryTime = DateTime.Now.AddSeconds(VerificationCodeExpiryTime), isValid = true, userId = User.id, verificationType = "RESETPASSWORD", }; using (var ctx = new PetWhizzEntities()) { ctx.userVerifications.Add(userVerificationInfo); ctx.SaveChanges(); } SendResetPasswordEmail(User.eMail, User.id, User.userName, GeneratedCode); } else { logger.Error("ForgotPassword request email is empty"); throw new CustomException("Email is empty", (int)ErrorCode.VALIDATIONFAILED); } } catch (CustomException) { throw; } catch (Exception ex) { logger.Error(MethodBase.GetCurrentMethod().Name + ": exception: " + ex.Message + ", " + ex.InnerException); throw new CustomException("SystemError", ex, (int)ErrorCode.PROCEESINGERROR); } }
//submit register page information to create account public ActionResult register(doctorAccountData doctor) { if (ModelState.IsValid) { try { doctor newDoctor = new doctor(); newDoctor.id = Guid.NewGuid(); string imageName = ""; //check if image is valid if (!validateProfessionImage(newDoctor.id.ToString(), ref imageName)) {//image is not valid ModelState.AddModelError("proImage", Resource1.professionImgNotValid); fillViewDataForDoctorRegistrationForm(doctor.cityID, doctor.destrictID, doctor.spID, doctor.subSpID); return(View()); } newDoctor.dateOfJoin = DateTime.Now; newDoctor.bookingType = doctor.bookingType; newDoctor.mail = doctor.mail; newDoctor.password = doctor.password; newDoctor.phone = doctor.phone; newDoctor.proImage = imageName; newDoctor.username = doctor.username; newDoctor.destrictID = doctor.destrictID; newDoctor.specialities = new List <speciality>(); //if the selected specialiy has sub specialities if (doctor.subSpID != null && db.specialities.Any(s => s.id == doctor.spID && s.subSpecialites.Count > 0)) {//the doctor selected sub specialities from main speciality foreach (string sp in doctor.subSpID.ToList()) { byte spID = byte.Parse(sp); newDoctor.specialities.Add(db.specialities.Find(spID)); } } else {//the doctor selected only main speciality newDoctor.specialities.Add(db.specialities.Find(doctor.spID)); } db.doctors.Add(newDoctor);//add new doctor account userVerification entry = new userVerification(); entry.userID = newDoctor.id; entry.verificationCode = Guid.NewGuid(); db.userVerifications.Add(entry);//add verification record db.SaveChanges(); //send email verification sendEmailVerification(entry.verificationCode.ToString(), newDoctor.mail, newDoctor.username); //add new doctor to role Membership.CreateUser(newDoctor.username, newDoctor.password, newDoctor.mail); Roles.AddUserToRole(newDoctor.username, "doctor"); if (isDoctorAuthenticated) { logout(); } addAuthenticationCookie(newDoctor.username, true); return(RedirectToAction("profile")); } catch (Exception) { fillViewDataForDoctorRegistrationForm(doctor.cityID, doctor.destrictID, doctor.spID, new string[] { "" }.ToList()); ModelState.AddModelError(string.Empty, Resource1.registerFaild); return(View()); } } else { //registeration data is not valid fillViewDataForDoctorRegistrationForm(doctor.cityID, doctor.destrictID, doctor.spID, doctor.subSpID); ModelState.AddModelError(string.Empty, Resource1.registerFaild); return(View()); } }
internal EnrollUserResponse EnrollUser(EnrollUserRequest EnrollUserRequest) { logger.Debug("Recived enroll user request"); EnrollUserResponse EnrollUserResponse; try { ValidateUserRequest ValidateUserRequest = new ValidateUserRequest() { email = EnrollUserRequest.email, username = EnrollUserRequest.username }; ValidatUser(ValidateUserRequest); EnrollUserRequest.username = Decryptor.Decrypt(EnrollUserRequest.username).Split('|')[1]; EnrollUserRequest.password = Decryptor.Decrypt(EnrollUserRequest.password).Split('|')[1]; EnrollUserRequest.email = Decryptor.Decrypt(EnrollUserRequest.email).Split('|')[1]; EnrollUserRequest.deviceId = Decryptor.Decrypt(EnrollUserRequest.deviceId).Split('|')[1]; String GeneratedToken = Guid.NewGuid().ToString(); int GeneratedCode = new Random().Next(100000, 999999); logger.Debug("Decrypted enroll user request details userName - " + EnrollUserRequest.username + " password - " + EnrollUserRequest.password + " email - " + EnrollUserRequest.email + " deviceId - " + EnrollUserRequest.deviceId); //validating details if (!String.IsNullOrEmpty(EnrollUserRequest.username) && !String.IsNullOrEmpty(EnrollUserRequest.password) && !String.IsNullOrEmpty(EnrollUserRequest.email) && !String.IsNullOrEmpty(EnrollUserRequest.deviceId)) { //setting up user details var user = new user() { createdDate = DateTime.Now, lastUpdatedDate = DateTime.Now, userName = EnrollUserRequest.username, password = EnrollUserRequest.password, eMail = EnrollUserRequest.email, status = "EMAILVERIFY" }; using (var ctx = new PetWhizzEntities()) { //saving user ctx.users.Add(user); ctx.SaveChanges(); //saving user device var userDevice = new userDevice() { deviceId = EnrollUserRequest.deviceId, // deviceName = EnrollUserRequest.deviceName, userId = user.id, }; ctx.userDevices.Add(userDevice); ctx.SaveChanges(); //saving user token var userToken = new userToken() { tokenType = "AUTHTOKEN", useCount = 0, generatedTime = DateTime.Now, userDeviceId = userDevice.id, expiryTime = DateTime.Now.AddSeconds(TokenExpiryTime), token = GeneratedToken, }; ctx.userTokens.Add(userToken); ctx.SaveChanges(); //user verification data var userVerificationInfo = new userVerification() { code = GeneratedCode.ToString(), generatedTime = DateTime.Now, expiryTime = DateTime.Now.AddSeconds(VerificationCodeExpiryTime), isValid = true, userId = user.id, verificationType = "EMAILVERIFY", }; ctx.userVerifications.Add(userVerificationInfo); ctx.SaveChanges(); } SendEmailVerification(user.eMail, user.userName, user.id, GeneratedCode.ToString()); EnrollUserResponse = new EnrollUserResponse() { token = Encryptor.Encrypt(DateTime.Now.ToString("M/d/yyyy h:mm:ss tt") + "|" + GeneratedToken), username = Encryptor.Encrypt(DateTime.Now.ToString("M/d/yyyy h:mm:ss tt") + "|" + user.userName), email = Encryptor.Encrypt(DateTime.Now.ToString("M/d/yyyy h:mm:ss tt") + "|" + user.eMail), status = Encryptor.Encrypt(DateTime.Now.ToString("M/d/yyyy h:mm:ss tt") + "|" + user.status), userId = Encryptor.Encrypt(DateTime.Now.ToString("M/d/yyyy h:mm:ss tt") + "|" + user.id) }; } else { logger.Error("Some of the properties in EnrollUserRequest is null or empty"); throw new CustomException("All propreties should contains a value", (int)ErrorCode.VALIDATIONFAILED); } } catch (CustomException) { throw; } catch (Exception ex) { logger.Error(MethodBase.GetCurrentMethod().Name + ": exception: " + ex.Message + ", " + ex.InnerException); throw new CustomException("SystemError", ex, (int)ErrorCode.PROCEESINGERROR); } return(EnrollUserResponse); }
internal void SendVerifyEmail() { logger.Debug("Recived SendVerifyEmail request"); try { CurrentUser currentUser = (CurrentUser)HttpContext.Current.User; if (String.IsNullOrEmpty(currentUser.token)) { logger.Error("Verify email token is invalid"); throw new CustomException(" token is invalid", (int)ErrorCode.UNAUTHORIZED); } using (var ctx = new PetWhizzEntities()) { using (var dbContextTransaction = ctx.Database.BeginTransaction()) { try { user User = ctx.users.Where(a => a.id == currentUser.userId).FirstOrDefault(); if (User == null) { logger.Error("Verify email user is invalid"); throw new CustomException("user is invalid", (int)ErrorCode.UNAUTHORIZED); } //inactive existing verifications for user List <userVerification> userVerificationList = ctx.userVerifications.Where(a => a.userId == User.id && a.verificationType == "EMAILVERIFY" && a.isValid == true).ToList(); foreach (userVerification userVerification in userVerificationList) { ctx.userVerifications.Attach(userVerification); userVerification.isValid = false; ctx.SaveChanges(); } int GeneratedCode = new Random().Next(100000, 999999); var userVerificationInfo = new userVerification() { code = GeneratedCode.ToString(), generatedTime = DateTime.Now, expiryTime = DateTime.Now.AddSeconds(VerificationCodeExpiryTime), isValid = true, userId = User.id, verificationType = "EMAILVERIFY", }; ctx.userVerifications.Add(userVerificationInfo); ctx.SaveChanges(); SendEmailVerification(User.eMail, User.userName, User.id, GeneratedCode.ToString()); dbContextTransaction.Commit(); } catch (Exception) { dbContextTransaction.Rollback(); dbContextTransaction.Dispose(); logger.Error("verification details update failed for userId - " + currentUser.userId.ToString()); throw new CustomException("verification details update failed", (int)ErrorCode.PROCEESINGERROR); } } } } catch (CustomException) { throw; } catch (Exception ex) { logger.Error(MethodBase.GetCurrentMethod().Name + ": exception: " + ex.Message + ", " + ex.InnerException); throw new CustomException("SystemError", ex, (int)ErrorCode.PROCEESINGERROR); } }