//Local register //Input: registerDTO //Output: clientId public static string registerClientLocal(registerDTO body) { SwapDbConnection db = new SwapDbConnection(); client client = db.clients.FirstOrDefault(c => c.email == body.email && (c.platform == "local")); string id = ""; if (client == null) { id = IdService.generateID("client"); HashSalt hs = HashSalt.GenerateSaltedHash(body.password); client new_client = new client() { client_id = id, email = body.email, birthday_date = body.birthday, creation_date = DateTime.Now, first_name = body.first_name, last_login = DateTime.Now, last_name = body.last_name, phone = body.phone, sex = body.sex, password = hs.Hash, salt = hs.Salt, platform = "local" }; db.clients.Add(new_client); db.SaveChanges(); } return(id); }
//Generate new password //Input: id, password //Output: string result public static string NewPassword(string id, string password) { SwapDbConnection db = new SwapDbConnection(); client client = db.clients.FirstOrDefault(c => c.client_id == id); HashSalt hashSalt; if (client == null || client.platform != "local") { return("false"); } if (HashSalt.VerifyPassword(password, client.password, client.salt)) { return("same"); } hashSalt = HashSalt.GenerateSaltedHash(password); client.password = hashSalt.Hash; client.salt = hashSalt.Salt; db.SaveChanges(); return("ok"); }
//Change user password //Input: clientId,password //Output: boolean result if the operation is successful public static bool ChangePassword(string clientId, string password) { SwapDbConnection db = new SwapDbConnection(); HashSalt newPasswordSalt; client user = db.clients.FirstOrDefault(c => c.client_id == clientId); if (user == null) { return(false); } if (user.platform != "local") { return(false); } newPasswordSalt = HashSalt.GenerateSaltedHash(password); user.salt = newPasswordSalt.Salt; user.password = newPasswordSalt.Hash; db.SaveChanges(); return(true); }