public void ChangePasswordTest() { string newPassword = "******"; var registration = new AccountRegistrationModel().GetTestModel(); var account = lykkeApi.Registration.PostRegistration(registration).Account; var passwordHash = new PasswordHashModel() { ClientId = account.Id, PwdHash = Sha256.GenerateHash(newPassword) }; var postChangeClientPassword = lykkeApi.ClientAccount.ClientAccountInformation .PostChangeClientPassword(passwordHash); Assert.That(postChangeClientPassword.StatusCode, Is.EqualTo(HttpStatusCode.OK)); Assert.That(lykkeApi.ClientAccount.ClientAccountInformation .PostAuthenticate(new ClientAuthenticationModel() { Email = registration.Email, Password = newPassword }).StatusCode, Is.EqualTo(HttpStatusCode.OK)); //TODO: Add more asserts }
public void CreateMember(CreateMemberModel model) { try { Members memberCreate = new Members { email = model.email, password = PasswordHashModel.Hash(model.password), firstname = model.password, lastname = model.lastname, position = model.position, role = model.role, created = DateTime.Now, updated = DateTime.Now }; this.onConvertBase64ToImage(memberCreate, model.image); this.db.Members.Add(memberCreate); this.db.SaveChanges(); } catch (Exception ex) { throw ex.GetErrorException(); } }
public void UpdateMember(int id, UpdateMamberModel model) { try { var memberUpdate = this.MemberItem.SingleOrDefault(m => m.id == id); if (memberUpdate == null) { throw new Exception("Note found member"); } this.db.Members.Attach(memberUpdate); memberUpdate.email = model.email; memberUpdate.firstname = model.firstname; memberUpdate.lastname = model.lastname; memberUpdate.position = model.position; memberUpdate.updated = DateTime.Now; memberUpdate.role = model.role; if (!string.IsNullOrEmpty(model.password)) { memberUpdate.password = PasswordHashModel.Hash(model.password); } this.onConverBase64ToImage(memberUpdate, model.image); this.db.Entry(memberUpdate).State = System.Data.Entity.EntityState.Modified; this.db.SaveChanges(); }catch (Exception e) { throw e.GetBaseException(); } }
public void ChangePassword(string email, ChangePasswordModel model) { try { var memberItem = db.Members.SingleOrDefault(item => item.email.Equals(email)); if (memberItem == null) { throw new Exception("Not found member"); } if (!PasswordHashModel.Verify(model.old_pass, memberItem.password)) { throw new Exception("Old-Password worng"); } else { this.db.Members.Attach(memberItem); memberItem.password = PasswordHashModel.Hash(model.new_pass); memberItem.updated = DateTime.Now; this.db.Entry(memberItem).State = System.Data.Entity.EntityState.Modified; this.db.SaveChanges(); } } catch (Exception ex) { throw ex.GetErrorException(); } }
public void Register(string username, string password, string profilePhotoUrl) { if (string.IsNullOrEmpty(username)) { throw new NullReferenceException("username cannot be null"); } if (string.IsNullOrEmpty(password)) { throw new NullReferenceException("password cannot be null"); } if (string.IsNullOrEmpty(profilePhotoUrl)) { throw new NullReferenceException("profilePhotoUrl cannot be null"); } PasswordHashModel hashModel = this.passwordHasher.CreatePasswordHash(password); var user = new VidconfileUser(); user.PasswordHash = hashModel.PasswordHash; user.PasswordSalt = hashModel.PasswordSalt; user.Username = username; user.ProfilePhotoUrl = profilePhotoUrl; this.userRepository.Add(user); this.unitOfWork.Commit(); }
public void ChangePasswordTest() { var passwordHash = new PasswordHashModel() { ClientId = account.Id, PwdHash = Sha256.GenerateHash(newPassword) }; var postChangeClientPassword = api.PostChangeClientPassword(passwordHash); Assert.That(postChangeClientPassword.StatusCode, Is.EqualTo(HttpStatusCode.OK)); }
public void CreatePasswordHash_ShouldCreate() { PasswordHasher hasher = new PasswordHasher(); string password = "******"; PasswordHashModel model = hasher.CreatePasswordHash(password); Assert.NotNull(model); Assert.NotNull(model.PasswordHash); Assert.NotNull(model.PasswordSalt); }
//เข้าสู่ระบบ public bool Login(LoginModel model) { try { var memberItem = this.db.Members.SingleOrDefault(m => m.email.Equals(model.email)); if (memberItem != null) { return(PasswordHashModel.Verify(model.password, memberItem.password)); } return(false); }catch (Exception e) { throw e.GetErrorException(); } }
public bool Login(LoginModel model) { try { var member = db.Members.SingleOrDefault(m => m.email == model.email); if (member != null) { return(PasswordHashModel.Verify(model.password, member.password)); } } catch (Exception ex) { throw ex; } return(false); }
public PasswordHashModel CreatePasswordHash(string password) { if (string.IsNullOrEmpty(password)) { throw new NullReferenceException("password cannot be null or empty"); } PasswordHashModel hashModel; using (var hmac = new HMACSHA512()) { hashModel = new PasswordHashModel(hmac.ComputeHash(Encoding.UTF8.GetBytes(password)), hmac.Key); } return(hashModel); }
public static PasswordHashModel GetHashAndSalt(string password) { PasswordHashModel passwordHashModel = new PasswordHashModel(); if (!string.IsNullOrEmpty(password)) { byte[] uniqueSalt = GetSalt(); byte[] hashedPassword = GetHashPassword(password, uniqueSalt); passwordHashModel.SaltText = Convert.ToBase64String(uniqueSalt); passwordHashModel.HashText = Convert.ToBase64String(hashedPassword); passwordHashModel.HashIteration = PBKDF2ITERATIONS; passwordHashModel.HashLength = hashedPassword.Length; } return(passwordHashModel); }
public IHttpActionResult PostRegister([FromBody] RegisterModel model) { if (ModelState.IsValid) { model.password = PasswordHashModel.Hash(model.password); try { this.Account.Register(model); return(Ok("Successful")); } catch (Exception ex) { ModelState.AddModelError("Exception", ex.Message); } } return(BadRequest(ModelState.GetErrorModelState())); }
public void Register_ShouldAddUser() { var videoRepositoryMock = new Mock <IRepository <Video> >(); var unitOfWorkMock = new Mock <IUnitOfWork>(); var commentRepositoryMock = new Mock <IRepository <Comment> >(); var userRepositoryMock = new Mock <IRepository <VidconfileUser> >(); var passwordHasherMock = new Mock <IPasswordHasher>(); var subscribeToSubscriberMock = new Mock <IRepository <SubscribeToSubscribers> >(); VidconfileUser user = null; userRepositoryMock.Setup(x => x.Add(It.IsAny <VidconfileUser>())) .Callback <VidconfileUser>(x => user = x); VidconfileUserServices userService = new VidconfileUserServices(userRepositoryMock.Object, unitOfWorkMock.Object, passwordHasherMock.Object, videoRepositoryMock.Object, subscribeToSubscriberMock.Object); string username = "******"; string password = "******"; string profilePhotoUrl = "nubfas"; PasswordHashModel hashModel = new PasswordHashModel(new byte[1], new byte[2]); passwordHasherMock.Setup(x => x.CreatePasswordHash(password)) .Returns(hashModel) .Verifiable(); userService.Register(username, password, profilePhotoUrl); passwordHasherMock.Verify(); unitOfWorkMock.Verify(x => x.Commit(), Times.Once); userRepositoryMock.Verify(x => x.Add(It.IsAny <VidconfileUser>()), Times.Once); Assert.Equal(username, user.Username); Assert.Equal(profilePhotoUrl, user.ProfilePhotoUrl); }
public IHttpActionResult PostGenerateMember() { try { var memberItems = new List <Member>(); var password = PasswordHashModel.Hash("123456"); var positions = new string[] { "Frontend Developer", "Backend Developer" }; var roles = new RoleAccount[] { RoleAccount.Member, RoleAccount.Employee, RoleAccount.Admin }; var random = new Random(); for (var index = 1; index <= 98; index++) { memberItems.Add(new Member { email = $"mail-{index}@mail.com", password = password, firstname = $"Firstname {index}", lastname = $"Lastname {index}", position = positions[random.Next(0, 2)], role = roles[random.Next(0, 3)], created = DateTime.Now, updated = DateTime.Now }); } var db = new DbEntities(); db.Members.AddRange(memberItems); db.SaveChanges(); return(Ok("Generate successful.")); } catch (Exception ex) { ModelState.AddModelError("Exceptrion", ex.Message); return(BadRequest(ModelState.GetErrorModelState())); } }
public IResponse PostChangeClientPassword(PasswordHashModel passwordHashModel) { return(Request.Post("/api/ClientAccountInformation/changeClientPassword") .AddJsonBody(passwordHashModel).Build().Execute()); }