public async Task <bool> Register(RegisterModel model, string link) { var isNotNew = _unitOfWork.Repository <UserEntity>().Set.Any(x => x.Id == model.Id && x.Email == model.Email); if (isNotNew) { return(false); } var salt = _cryptoContext.GenerateSaltAsBase64(); var password = Convert.ToBase64String(_cryptoContext.DeriveKey(model.Password, salt)); var user = new UserEntity { Id = model.Id, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Role = RoleEnum.User, Created = DateTime.Now, Password = password, Salt = salt, Confirmation = new ConfirmationEntity { Code = Guid.NewGuid().ToString(), Confirmed = false, Id = model.Id, } }; _unitOfWork.Repository <UserEntity>().Insert(user); await _emailSender.SendEmail(user.Email, "Account Confirmation", "ConfirmAccount", new ConfirmEmail { Link = $"{link}/{user.Confirmation.Code}" }); return(true); }
public SystemDetailsModel UpdateSystemDetails(SystemDetailsModel model) { var entity = _unitOfWork.Repository <UserEntity>().Set .FirstOrDefault(x => x.Id == _authUser.Id); entity.FirstName = model.FirstName; entity.LastName = model.LastName; if (entity.Password != model.Password) { var salt = _cryptoContext.GenerateSaltAsBase64(); var password = Convert.ToBase64String(_cryptoContext.DeriveKey(model.Password, salt)); entity.Salt = salt; entity.Password = password; } _unitOfWork.Repository <UserEntity>().Update(entity); return(model); }
public TokenModel ResetPasword(ResetPaswordModel model) { var token = _unitOfWork.Repository <ForgotPaswordTokenEntity>().Include(x => x.User).FirstOrDefault(x => x.Code == model.Code && !x.Used); if (token != null) { var salt = _cryptoContext.GenerateSaltAsBase64(); var password = Convert.ToBase64String(_cryptoContext.DeriveKey(model.Password, salt)); token.User.Salt = salt; token.User.Password = password; token.Used = true; _unitOfWork.Repository <UserEntity>().Update(token.User); _unitOfWork.Repository <ForgotPaswordTokenEntity>().Update(token); return(_authTokenProvider.GetToken(token.User)); } return(null); }
public RegisteredUserModel Register(UserRegistrationModel model) { if (unitOfWork.Repository <UserEntity>().Set.Any(x => x.Email == model.Email)) { return(null); } string salt = cryptoContext.GenerateSaltAsBase64(); var user = new UserEntity { Id = Guid.NewGuid(), Name = $"{model.Firstname} {model.Lastname}", DateOfBirth = model.DateOfBirth, Email = model.Email, Type = model.UserType, Password = Convert.ToBase64String(cryptoContext.DeriveKey(model.Password, salt)), Salt = salt, Receivers = new List <ReviewReceiverEntity>() }; user.Receivers.Add(new ReviewReceiverEntity { Id = Guid.NewGuid() }); unitOfWork.Repository <UserEntity>().Insert(user); unitOfWork.SaveChanges(); return(new RegisteredUserModel { Id = user.Id, FullName = user.Name, Token = tokenService.GetToken(new LoginCredentials { Email = user.Email, GrantType = "password", Password = model.Password }) }); }