public static string addUpdateUser(user u) { try { using (var db = new etronEntities()) { if (u.id == 0) { using (MD5 md5Hash = MD5.Create()) { string hash = GetMd5Hash(md5Hash, u.pass); u.pass = hash; db.users.Add(u); } } else { // Keep old password if (string.IsNullOrEmpty(u.pass)) { var existU = db.users.Find(u.id); if (existU == null) { return("Thất bại: Không tìm thấy user"); } existU.name = u.name; db.Entry(existU).State = EntityState.Modified; } else { // change new pass using (MD5 md5Hash = MD5.Create()) { string hash = GetMd5Hash(md5Hash, u.pass); u.pass = hash; db.Entry(u).State = EntityState.Modified; } } } db.SaveChanges(); } return(string.Empty); } catch (Exception ex) { return("Thất bại: " + ex.Message); } }
public static string deleteUser(int uId) { try { using (var db = new etronEntities()) { var u = new user() { id = uId }; db.Entry(u).State = EntityState.Deleted; db.SaveChanges(); } return(string.Empty); } catch (Exception ex) { return("Thất bại: " + ex.Message); } }
public static user validateLogin(LoginModel model) { user u = null; try { using (var db = new etronEntities()) { using (MD5 md5Hash = MD5.Create()) { string hash = GetMd5Hash(md5Hash, model.Password); u = db.users.Where(f => f.name.Equals(model.Username) && f.pass.Equals(hash)).FirstOrDefault(); } } return(u); } catch (Exception ex) { return(u); } }