/// <summary> /// 修改月用户密码 /// </summary> /// <param name="id">用户登录ID</param> /// <param name="od">旧密码</param> /// <param name="nd">新密码</param> /// <returns></returns> public OperationResult ChangePasswrod(Guid id, string od, string nd) { if (!UserLoginRepository.CheckExists(m => m.Id == id)) { throw new Exception("id:未查询到用户信息"); } else { var result = UserLoginRepository.TrackEntities.First(m => m.Id == id); if (result.Password != od.AESEncrypt128(result.SecretKey)) { throw new Exception("id:验证旧密码出错"); } else { result.SecretKey = new Random().NextLetterString(16).ToUpper(); result.Password = nd.AESEncrypt128(result.SecretKey); if (UserLoginRepository.Update(result) > 0) { //修改密码后移除登录缓存 ICache iCache = CacheManager.GetCacher <CacheUser>(); iCache.Remove(result.UserName.AESEncrypt128()); return(new OperationResult(OperationResultType.Success, $"修改用户{result.UserName}密码成功")); } else { throw new Exception($"id:修改用户{result.UserName}密码失败"); } } } }
public OperationResult UpdatedUser(UserInputDto inputDto) { if (!UserLoginRepository.CheckExists(m => m.Id == inputDto.Id)) { throw new Exception("id:用户信息不存在"); } else { var result = UserLoginRepository.TrackEntities.First(m => m.Id == inputDto.Id); var result_user = result.UserMany.First(); result_user.Email = inputDto.Email; result_user.PhoneNumber = inputDto.PhoneNumber; result_user.WeChat = inputDto.WeChat; result_user.Remark = inputDto.Remark; result_user.Theme = inputDto.Theme; result_user.RealName = inputDto.RealName; result_user.NickName = inputDto.NickName; result_user.Sex = inputDto.Sex; result_user.IsAlarm = inputDto.IsAlarm; result_user.IsSysReceive = inputDto.IsSysReceive; result_user.Language = inputDto.Language; result_user.LastUpdatedTime = DateTime.Now; result_user.LastUpdatorUserId = result.UserName; int n = UserLoginRepository.Update(result); if (n > 0) { return(new OperationResult(OperationResultType.Success, "更新用户基本信成功!")); } else { throw new Exception("id:更新用户数据失败"); } } }
/// <summary> /// 更新用户头像 /// </summary> /// <param name="id">用户主键ID</param> /// <param name="imageFile">图片文件</param> /// <returns>是否修改成功</returns> public async Task <OperationResult> SetUserHeadIcon(Guid id, Task <FileInfo> imageFile) { if (!UserLoginRepository.CheckExists(m => m.Id == id)) { throw new Exception($"id:用户主键{id}的账号不存在"); } else { var file = await imageFile; using (Bitmap bitmap = new Bitmap(file.FullName)) { byte[] HeadIcomBits = bitmap.ToBytes(); var user = UserLoginRepository.TrackEntities.FirstOrDefault(m => m.Id == id); user.UserMany.First().HeadIcon = HeadIcomBits; user.UserMany.First().HeadIconPath = Path.Combine("~/HeadIconFiles", file.Name); if (UserLoginRepository.Update(user) > 0) { return(new OperationResult(OperationResultType.Success, "操作成功!")); } else { return(new OperationResult(OperationResultType.Error, "操作失败!")); } } } }
/// <summary> /// 获取用户头像byte[] /// </summary> /// <param name="id">用户主键</param> /// <returns></returns> public byte[] GetHeadIconBits(Guid id) { if (!UserLoginRepository.CheckExists(m => m.Id == id)) { throw new Exception($"id:查询信息不能存在"); } else { var result = UserLoginRepository.TrackEntities.First(m => m.Id == id); var user = result.UserMany.First(); if (string.IsNullOrEmpty(user.HeadIconPath) && (user.HeadIcon?.Length <= 0 || user.HeadIcon == null)) { return(Properties.Resources._default.ToBytes()); } else { var imgPath = HttpContext.Current.Server.MapPath($"{user.HeadIconPath}"); if (File.Exists(imgPath)) { return(File.ReadAllBytes(imgPath)); } else { imgPath = HttpContext.Current.Server.MapPath($"~/HeadIconFiles\\{result.Id.ToString()}"); string str = user.HeadIcon.CreateImageFromBytes(imgPath); user.HeadIconPath = $"~/HeadIconFiles\\{str.Substring(str.LastIndexOf('\\') + 1)}"; UserLoginRepository.Update(result); return(user.HeadIcon); } } } }
/// <summary> /// 用户进行登录检查 /// </summary> /// <param name="UserName">用户帐号</param> /// <param name="Passwork">用户密码</param> /// <returns>用户登录成功的关联集合</returns> public OperationResult CheckToUserLogin(string userName, string passwork) { if (!UserLoginRepository.CheckExists(m => m.UserName == userName)) { throw new Exception($"id:{userName}帐号不存在"); } else { var user = UserLoginRepository.TrackEntities.FirstOrDefault(m => m.UserName == userName); var diffTime = DateTime.Now.Subtract(user.LockoutEndDateUtc).TotalMinutes; // 判断帐号是否被登录锁定 if (user.LockoutEnabled && user.AccessFailedCount >= 15 && diffTime < 0) { throw new Exception($"id:帐号当前登录已被锁定,请在{user.LockoutEndDateUtc}后重试"); } string _passwork = passwork.AESEncrypt128(user.SecretKey); if (user.Password != _passwork) { if (user.LockoutEnabled) { //密码锁时间已经过去,重置密码锁相关信息 if (diffTime > 10) { user.LockoutEndDateUtc = DateTime.Now; user.AccessFailedCount = 1; UserLoginRepository.Update(user); throw new Exception($"id:登录密码错误,离帐号被登录锁定还剩{15 - user.AccessFailedCount}次机会"); } else { user.LockoutEndDateUtc = DateTime.Now; if (++user.AccessFailedCount >= 15) { user.LockoutEndDateUtc = DateTime.Now.AddMinutes(10); } UserLoginRepository.Update(user); throw new Exception($"id:登录密码错误,离帐号被登录锁定还剩{15 - user.AccessFailedCount}次机会"); } } else { throw new Exception($"id:{userName}输入的帐号密码有误!"); } } else { //用户是否被冻结 if (user.IsLocked) { throw new Exception($"id:{userName}帐号已经被冻结,请联系管理员解封!"); } user.AccessFailedCount = 0; user.LockoutEndDateUtc = DateTime.Now; user.LockoutEndDateUtc = DateTime.Now; user.LoginCount++; user.FirstVisitTime = user.FirstVisitTime.Year == 1 ? DateTime.Now : user.FirstVisitTime; user.PreviousVisitTime = user.LastVisitTime; user.LastVisitTime = DateTime.Now; UserLoginRepository.Update(user); UserLoginOutDto ulod = user.MapTo <UserLoginOutDto>(); var topInfo20 = ulod.InformationMany.OrderBy(m => m.CreatedTime).Take(10).ToList(); ulod.InformationMany.Clear(); //获取用户的通知信息的一些关联属性 topInfo20.ForEach( o => { switch (o) { case InformationOutDto i when i.TypeIndex >= 0 && i.TypeIndex <= 10: var objResult1 = HostRepository.Entities.Where(m => m.Id == o.ObjectId).ToList(); if (objResult1.Count > 0) { o.ObjectResult = objResult1.Select(m => new { OrganizeName = m.OrganizeOne.FullName, RegPackage = m.RegPackage, HostFullName = m.FullName }).First(); } break; case InformationOutDto i when i.TypeIndex >= 11 && i.TypeIndex <= 20: var objResult2 = SubRepository.Entities.Where(m => m.Id == o.ObjectId).ToList(); if (objResult2.Count > 0) { o.ObjectResult = objResult2.Select(m => new { OrganizeName = m.SubAggregationMany.Count > 0 ? m.SubAggregationMany.First().OrganizeOne.FullName : null, RegPackage = m.SubAggregationMany.Count > 0 ? m.SubAggregationMany.First().HostOne.RegPackage : null, HostFullName = m.SubAggregationMany.Count > 0 ? m.SubAggregationMany.First().HostOne.FullName : null, SubNum = m.SubNum, FullName = m.SubName, }).First(); } break; } ulod.InformationMany.Add(o); }); ulod.UserMany.First().HeadIconPath = ulod.Id.ToString().AESEncrypt128(); ulod.OrganizeOne.OrganizeLogoPath = ulod.OrganizeOne.Id.ToString().AESEncrypt128(); //设置缓存 ICache iCache = CacheManager.GetCacher <CacheUser>(); int.TryParse($"LoginTimeOut".GetValue(), out int cacheTimeOut); cacheTimeOut = cacheTimeOut == 0 ? 30 : cacheTimeOut; iCache.Set( key: user.UserName.AESEncrypt128(), value: new CacheUser(user.Id, user.UserName, user.SecretKey, user.IsAdministrator, user.Level, user.Organize_Id), slidingExpiration: TimeSpan.FromMinutes(cacheTimeOut)); //返回结果 return(new OperationResult { ResultType = OperationResultType.Success, Message = $"{user.SecretKey}", Data = ulod }); } } }