private long AddUserLoginState(BCEnterpriseContext db, Account.Dtos.UserLoginStateDto model) { if (model == null) { throw new ArgumentNullException("UserLoginStateDto"); } var temp = db.UserLoginStates.FirstOrDefault(m => m.UserID == model.UserID && m.Device == model.Device) ?? new UserLoginState(); temp.UserID = model.UserID; temp.Device = model.Device; temp.UserName = model.UserName; temp.LoginToken = model.LoginToken; temp.LoginIP = model.LoginIP; temp.LoginTime = DateTime.Now; if (temp.UserLoginStateID == 0) { db.UserLoginStates.Add(temp); } else { db.Entry <UserLoginState>(temp).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); model.UserLoginStateID = temp.UserLoginStateID; return(temp.UserLoginStateID); }
public bool UpdateUserLoginState(string userId, string lastIP, string Device, string LoginToken) { try { if (string.IsNullOrEmpty(userId)) { throw new ArgumentNullException("userId"); } if (string.IsNullOrEmpty(lastIP)) { throw new ArgumentNullException("lastIP"); } using (var db = new BCEnterpriseContext()) { var user = db.FrontUsers.First(n => n.UserID == userId); user.LastDate = DateTime.Now; user.LastIP = lastIP; db.Entry <FrontUser>(user).State = System.Data.Entity.EntityState.Modified; var updateResult = db.SaveChanges() > 0; var setLoginStateResult = AddUserLoginState(db, new UserLoginStateDto() { Device = Device, LoginIP = lastIP, LoginTime = user.UpdateTime, LoginToken = LoginToken, UserID = user.UserID, UserName = user.Name }); LoginLogWrite(db, new UserLoginLog { UserID = user.UserID, UserName = user.Name, IP = lastIP, Device = Device, Time = user.UpdateTime, Description = string.Empty, Status = (int)EnterpriseData.Common.LoginStatus.Login }); return(updateResult && setLoginStateResult > 0); } } catch (Exception ex) { throw ex; } }
public bool UpdateUserPassword(string userId, string oldPass, string newPass) { try { if (string.IsNullOrEmpty(userId) && string.IsNullOrEmpty(oldPass) && string.IsNullOrEmpty(newPass)) { throw new KnownException("缺少必要信息,无法更新密码!"); } using (var db = new BCEnterpriseContext()) { var q = db.FrontUsers.FirstOrDefault(obj => obj.UserID == userId); if (null == q) { throw new KnownException("用户不存在!"); } if (oldPass == CryptoService.MD5Decrypt(q.Password)) { q.Password = CryptoService.MD5Encrypt(newPass); db.Entry(q).State = EntityState.Modified; if (0 < db.SaveChanges()) { return(true); } } else { throw new KnownException("原密码错误,修改密码失败!"); } return(false); } } catch (Exception ex) { throw ex; } }