public UserDto Create(UserDto user) { User newUser; using (var db = new OnlineCasinoDb()) { if (db.Users.FirstOrDefault(u => object.Equals(u.Username, user.Username)) != null) { throw new ConflictException(); } newUser = new User() { FullName = user.FullName, Email = user.Email, Money = 0 }; CryptographicManager.SetNewUserInfo(newUser, user.Username, user.Password); db.Users.Add(newUser); db.SaveChanges(); } return(CreateUserDTO(newUser)); }
public void UpdatePassword(int id, string oldPassword, string newPassword) { using (var db = new OnlineCasinoDb()) { var userForUpdate = db.Users.FirstOrDefault(u => u.Id == id); if (userForUpdate == null) { throw new NotFoundException(); } var saltedOldPassword = CryptographicManager.GenerateSHA256Hash(oldPassword, userForUpdate.Salt); if (!object.Equals(userForUpdate.Password, saltedOldPassword)) { throw new BadRequestException(); } var saltedNewPassword = CryptographicManager.GenerateSHA256Hash(newPassword, userForUpdate.Salt); userForUpdate.Password = saltedNewPassword; db.Users.AddOrUpdate(userForUpdate); db.SaveChanges(); } }
public bool IsPasswordCorrect(int id, string password) { using (var db = new DiceGamingDb()) { var user = db.Users.FirstOrDefault(u => u.Id == id); if (user == null) throw new BadRequestException(); var saltedPassword = CryptographicManager.GenerateSHA256Hash(password, user.Salt); return object.Equals(user.Password, saltedPassword); } }
public UserDto Get(string username, string password) { User user; using (var db = new DiceGamingDb()) { user = db.Users.FirstOrDefault(u => object.Equals(u.Username, username)); if (user == null) throw new NotFoundException(); var saltedPassword = CryptographicManager.GenerateSHA256Hash(password, user.Salt); if (!object.Equals(user.Password, saltedPassword)) throw new BadRequestException(); } return CreateUserDTO(user); }
public void SetUp() { // Configure server and client var config = new HttpConfiguration(); WebApiConfig.Register(config); TestHelper.Server = new HttpServer(config); TestHelper.Client = new HttpClient(TestHelper.Server); // Make sure a test user exists in database if not create it TestHelper.TestUser = new UserDto() { Username = "******", Password = Convert.ToBase64String(Guid.NewGuid().ToByteArray()), FullName = "Test Testov", Email = "*****@*****.**", Money = 9999 }; User user; using (var db = new OnlineCasinoDb()) { user = db.Users.FirstOrDefault(u => object.Equals(u.Username, TestHelper.TestUser.Username)); if (user == null) { user = new User(); } CryptographicManager.SetNewUserInfo(user, TestHelper.TestUser.Username, TestHelper.TestUser.Password); user.Username = TestHelper.TestUser.Username; user.FullName = TestHelper.TestUser.FullName; user.Email = TestHelper.TestUser.Email; user.Money = TestHelper.TestUser.Money; db.Users.AddOrUpdate(user); db.SaveChanges(); } TestHelper.TestUser.Id = user.Id; // save user id // Create test login TestHelper.TestLogin = new LoginDto() { UserId = TestHelper.TestUser.Id, Token = Convert.ToBase64String(Guid.NewGuid().ToByteArray()) }; Login login; using (var db = new OnlineCasinoDb()) { login = new Login() { UserId = TestHelper.TestLogin.UserId, Token = TestHelper.TestLogin.Token }; db.Logins.Add(login); db.SaveChanges(); } TestHelper.TestLogin.Id = login.Id; // save login id }