public int ForgotPassword(string userEmail) { logger.Info("Inside UserService/ForgotPassword"); try { int result = 0; string password = String.Empty; string salt = String.Empty; var userDetail = unitOfWork.DbContext.UserDetail .Where(ud => String.Compare(ud.Email, userEmail, true) == 0).FirstOrDefault(); if (userDetail != null) { password = PasswordManager.GeneratePassword1(); string pwdHash = PasswordManager.GeneratePasswordHash(password, out salt); userDetail.Salt = salt; userDetail.Password = pwdHash; // save to db password userDetail.SetByAdmin = false; var resetMapping = unitOfWork.DbContext.PasswordResetMapping.Where(prs => prs.UserId == userDetail.ID).FirstOrDefault(); if (resetMapping != null) { resetMapping.PasswordResetOn = DateTime.UtcNow; } else { PasswordResetMapping passwordResetMapping = new PasswordResetMapping() { UserId = userDetail.ID, PasswordResetOn = DateTime.UtcNow, }; unitOfWork.DbContext.PasswordResetMapping.Add(passwordResetMapping); } unitOfWork.DbContext.SaveChanges(); SendMail.SendMailForForgotPassword(userEmail, password); result = 1; logger.Info("Record successfully inserted"); } else { throw new NoDataFoundException(); } return(result); } catch (BaseException ex) { throw ex; } catch (Exception ex) { throw ex; } }
public int AddNewUser(List <UserInfoModel> userInfoModel, int permission) { int result = 0; List <String> mailIdsList = new List <string>(); List <String> passwordList = new List <string>(); try { if (permission == 1) { foreach (UserInfoModel userInfo in userInfoModel) { var userIsPresent = unitOfWork.DbContext.UserDetail .Any(ud => String.Compare(ud.Email, userInfo.Email, true) == 0 ); if (userIsPresent) { throw new UserAlreadyPresentException(); } else { userInfo.PasswordToMail = PasswordManager.GeneratePassword1(); string salt = String.Empty; userInfo.Password = PasswordManager.GeneratePasswordHash(userInfo.PasswordToMail, out salt); UserDetail userDetail = new UserDetail { Name = userInfo.FirstName + " " + userInfo.LastName, Email = userInfo.Email, Password = userInfo.Password, //RegisteredDate = DateTime.UtcNow, UserTelephone = userInfo.Contact, CompanyName = userInfo.Company, Dob = userInfo.DOB, Salt = salt, SetByAdmin = true }; //masterContext.UserDetail.Add(userDetail); PermissionLevelMapping permissionMappingTable = new PermissionLevelMapping() { UserDetail = userDetail, PermissionId = userInfo.Permission, CreationDate = DateTime.UtcNow }; PasswordResetMapping passwordResetMapping = new PasswordResetMapping() { UserDetail = userDetail, PasswordResetOn = DateTime.UtcNow, }; unitOfWork.DbContext.PermissionLevelMappings.Add(permissionMappingTable); unitOfWork.DbContext.PasswordResetMapping.Add(passwordResetMapping); mailIdsList.Add(userDetail.Email); passwordList.Add(userInfo.PasswordToMail); } } unitOfWork.DbContext.SaveChanges(); foreach (var mp in mailIdsList.Zip(passwordList, Tuple.Create)) { SendMail.SendMailForRegistration(mp.Item1, mp.Item2); } result = 1; return(result); } else { throw new PermissionDeniedException(); } } catch (UserServiceException ex) { throw ex; } catch (BaseException ex) { throw ex; } catch (Exception ex) { throw ex; } }