public LoginInfo GetLoginInfo(Guid token) { return CacheHelper.Get<LoginInfo>(string.Format(_LoginInfoKeyFormat, token), () => { using (var dbContext = new AccountDbContext()) { //如果有超时的,启动超时处理 var timeoutList = dbContext.FindAll<LoginInfo>(p => DbFunctions.DiffMinutes(DateTime.Now, p.LastAccessTime) > _UserLoginTimeoutMinutes); if (timeoutList.Count > 0) { foreach (var li in timeoutList) dbContext.LoginInfos.Remove(li); } dbContext.SaveChanges(); var loginInfo = dbContext.FindAll<LoginInfo>(l => l.LoginToken == token).FirstOrDefault(); if (loginInfo != null) { loginInfo.LastAccessTime = DateTime.Now; dbContext.Update<LoginInfo>(loginInfo); } return loginInfo; } }); }
public LoginInfo Login(string loginName, string password) { LoginInfo loginInfo = null; password = Encrypt.MD5(password); loginName = loginName.Trim(); using (var dbContext = new AccountDbContext()) { var user = dbContext.Users.Include("Roles").Where(u => u.LoginName == loginName && u.Password == password && u.IsActive).FirstOrDefault(); if (user != null) { var ip = Fetch.UserIp; loginInfo = dbContext.FindAll<LoginInfo>(p => p.LoginName == loginName && p.ClientIP == ip).FirstOrDefault(); if (loginInfo != null) { loginInfo.LastAccessTime = DateTime.Now; } else { loginInfo = new LoginInfo(user.ID, user.LoginName); if (user.Roles != null && user.Roles.Count > 0 && user.Roles.Exists(u => u.Name == "系统管理员")) //判断是否系统管理员 loginInfo.EnumLoginAccountType = (int)EnumLoginAccountType.Administrator; loginInfo.ClientIP = ip; loginInfo.BusinessPermissionList = user.BusinessPermissionList; dbContext.Insert<LoginInfo>(loginInfo); } } } return loginInfo; }
public void Logout(Guid token) { using (var dbContext = new AccountDbContext()) { var loginInfo = dbContext.FindAll<LoginInfo>(l => l.LoginToken == token).FirstOrDefault(); if (loginInfo != null) { dbContext.Delete<LoginInfo>(loginInfo); } } CacheHelper.Remove(string.Format(_LoginInfoKeyFormat, token)); }
public void SaveUser(User user) { using (var dbContext = new AccountDbContext()) { if (user.ID > 0) { dbContext.Update<User>(user); var roles = dbContext.Roles.Where(r => user.RoleIds.Contains(r.ID)).ToList(); user.Roles = roles; dbContext.SaveChanges(); } else { var existUser = dbContext.FindAll<User>(u => u.LoginName == user.LoginName); if (existUser.Count > 0) { throw new BusinessException("LoginName", "此登录名已存在!"); } else { dbContext.Insert<User>(user); var roles = dbContext.Roles.Where(r => user.RoleIds.Contains(r.ID)).ToList(); user.Roles = roles; dbContext.SaveChanges(); } } } }
public bool CheckVerifyCode(string verifyCodeText, Guid guid) { using (var dbContext = new AccountDbContext()) { var verifyCode = dbContext.FindAll<VerifyCode>(v => v.Guid == guid && v.VerifyText == verifyCodeText).LastOrDefault(); if (verifyCode != null) { dbContext.VerifyCodes.Remove(verifyCode); dbContext.SaveChanges(); //清除验证码大于2分钟还没请求的 var expiredTime = DateTime.Now.AddMinutes(-2); dbContext.VerifyCodes.Where(v => v.CreateTime < expiredTime).Delete(); return true; } else { return false; } } }