public string Create(string email, string password) { if (String.IsNullOrWhiteSpace(email) || String.IsNullOrWhiteSpace(password)) { throw new Exception("Email or password incorrect."); } var emailLower = email.ToLower().Trim(); var user = db.User.SingleOrDefault(x => x.Email.Equals(emailLower)); if (user == null) { throw new Exception("Email or password incorrect."); } var passwordCrypt = Crypt.Encrypt(user.Email + "*" + password); if (!user.Password.Equals(passwordCrypt)) { throw new Exception("Email or password incorrect."); } var session = new UserSession { Token = Guid.NewGuid().ToString(), User = user, DateCreate = DateTime.Now }; db.UserSession.Add(session); db.SaveChanges(); return(session.Token); }
public void SignUp(User user) { if (string.IsNullOrWhiteSpace(user.Email) || string.IsNullOrWhiteSpace(user.Password)) { throw new Exception("Incorrect username or password."); } if (user.Password.Length < 8) { throw new Exception("Min 8 characters."); } user.Email = user.Email.ToLower().Trim(); if (db.User.Any(x => x.Email.Equals(user.Email))) { throw new Exception("User already exists."); } user.Password = Crypt.Encrypt(user.Email + "*" + user.Password); user.DateCreate = DateTime.Now; db.User.Add(user); db.SaveChanges(); }
public void Update(int idUser, string oldPassword, string newPassword) { if (idUser == 0 || string.IsNullOrWhiteSpace(oldPassword) || string.IsNullOrWhiteSpace(newPassword)) { throw new Exception("Fields required."); } if (newPassword.Length < 8) { throw new Exception("Min 8 characters."); } var user = db.User.Find(idUser); if (user == null) { throw new Exception("Not found user."); } var passwordCrypt = Crypt.Encrypt(user.Email + "*" + oldPassword); if (!user.Password.Equals(passwordCrypt)) { throw new Exception("Not found user."); } user.Password = Crypt.Encrypt(user.Email + "*" + newPassword); db.SaveChanges(); }
public int Create(MessageDto message) { message.DateCreate = DateTime.Now; db.Message.Add(Convert(message)); var id = db.SaveChanges(); return(id); }