From 6c443ca08913ac71218dfeaf9a7d02ff7b8fe36d Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Fri, 6 Sep 2024 14:13:03 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E7=BD=AE=E4=B8=BA=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ExtraController.cs | 2 +- .../Helper/EmailMaskHelper.cs | 44 ---------- .../Helper/IRCEmailPasswordHelper.cs | 85 +++++++++++++++++++ .../Service/Management/UserService.cs | 20 +---- .../SiteSurvey/TrialSiteSurveyService.cs | 2 + .../TrialSiteUser/TrialExternalUserService.cs | 3 +- IRaCIS.Core.Application/TestService.cs | 7 +- 7 files changed, 95 insertions(+), 68 deletions(-) delete mode 100644 IRaCIS.Core.Application/Helper/EmailMaskHelper.cs create mode 100644 IRaCIS.Core.Application/Helper/IRCEmailPasswordHelper.cs diff --git a/IRaCIS.Core.API/Controllers/ExtraController.cs b/IRaCIS.Core.API/Controllers/ExtraController.cs index f9b40bb88..59076ec3e 100644 --- a/IRaCIS.Core.API/Controllers/ExtraController.cs +++ b/IRaCIS.Core.API/Controllers/ExtraController.cs @@ -231,7 +231,7 @@ namespace IRaCIS.Api.Controllers var email = returnModel.Data.BasicInfo.EMail; - var hiddenEmail = EmailMaskHelper.MaskEmail(email); + var hiddenEmail = IRCEmailPasswordHelper.MaskEmail(email); returnModel.Data.BasicInfo.EMail = hiddenEmail; diff --git a/IRaCIS.Core.Application/Helper/EmailMaskHelper.cs b/IRaCIS.Core.Application/Helper/EmailMaskHelper.cs deleted file mode 100644 index 027c74523..000000000 --- a/IRaCIS.Core.Application/Helper/EmailMaskHelper.cs +++ /dev/null @@ -1,44 +0,0 @@ -using NPOI.SS.Formula.Functions; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace IRaCIS.Core.Application.Helper -{ - public static class EmailMaskHelper - { - - //显示位数:3分之2的位数,向上取整 - //取哪几个个值:最后一位和前面几位 - //其他:3个***。 - //比如:hlj23@126.com - //为:hlj***3@126.com - - //he@126.com - //为:h*** e@126.com - - public static string MaskEmail(string email) - { - - // 找到 "@" 符号的位置 - int atIndex = email.IndexOf('@'); - - string visiblePartBefore = email.Substring(0, atIndex); - - string afterAt = email.Substring(atIndex + 1); - - int visibleLength = (int)Math.Ceiling((double)visiblePartBefore.Length * 2 / 3); - - // 替换中间两位字符为星号 - string hiddenPartBeforeAt = visiblePartBefore.Substring(0, visibleLength - 1) + "***" + visiblePartBefore.Last(); - - - // 组合隐藏和可见部分 - string hiddenEmail = hiddenPartBeforeAt + "@" + afterAt; - - return hiddenEmail; - } - } -} diff --git a/IRaCIS.Core.Application/Helper/IRCEmailPasswordHelper.cs b/IRaCIS.Core.Application/Helper/IRCEmailPasswordHelper.cs new file mode 100644 index 000000000..64ec7309f --- /dev/null +++ b/IRaCIS.Core.Application/Helper/IRCEmailPasswordHelper.cs @@ -0,0 +1,85 @@ +using NPOI.SS.Formula.Functions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Application.Helper +{ + public static class IRCEmailPasswordHelper + { + private static readonly Random Random = new Random(); + + //显示位数:3分之2的位数,向上取整 + //取哪几个个值:最后一位和前面几位 + //其他:3个***。 + //比如:hlj23@126.com + //为:hlj***3@126.com + + //he@126.com + //为:h*** e@126.com + + public static string MaskEmail(string email) + { + + // 找到 "@" 符号的位置 + int atIndex = email.IndexOf('@'); + + string visiblePartBefore = email.Substring(0, atIndex); + + string afterAt = email.Substring(atIndex + 1); + + int visibleLength = (int)Math.Ceiling((double)visiblePartBefore.Length * 2 / 3); + + // 替换中间两位字符为星号 + string hiddenPartBeforeAt = visiblePartBefore.Substring(0, visibleLength - 1) + "***" + visiblePartBefore.Last(); + + + // 组合隐藏和可见部分 + string hiddenEmail = hiddenPartBeforeAt + "@" + afterAt; + + return hiddenEmail; + } + + /// + /// 密码必须包含:1)8 – 32 个字符;2)至少1个数字;3) 至少1个大写字母;4)至少1个小写字母;5)至少1个特殊字符 (~!-@#$%^&*_+?) + /// + /// + public static string GenerateRandomPassword(int length) + { + // 必须包含的字符组 + const string numbers = "0123456789"; + const string upperCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const string lowerCaseLetters = "abcdefghijklmnopqrstuvwxyz"; + const string specialCharacters = "~!-@#$%^&*_+?"; + + // 随机选择至少一个字符 + char[] requiredCharacters = + { + numbers[Random.Next(numbers.Length)], + upperCaseLetters[Random.Next(upperCaseLetters.Length)], + lowerCaseLetters[Random.Next(lowerCaseLetters.Length)], + specialCharacters[Random.Next(specialCharacters.Length)] + }; + + // 构建剩余的字符集,用于填充密码的其余部分 + string allCharacters = numbers + upperCaseLetters + lowerCaseLetters + specialCharacters; + + // 确保密码长度满足用户要求 + char[] password = new char[length]; + + // 将必须包含的字符放入密码中 + requiredCharacters.CopyTo(password, 0); + + // 填充剩余的字符 + for (int i = requiredCharacters.Length; i < length; i++) + { + password[i] = allCharacters[Random.Next(allCharacters.Length)]; + } + + // 随机打乱密码字符顺序 + return new string(password.OrderBy(_ => Random.Next()).ToArray()); + } + } +} diff --git a/IRaCIS.Core.Application/Service/Management/UserService.cs b/IRaCIS.Core.Application/Service/Management/UserService.cs index 06fda2da9..9048c5581 100644 --- a/IRaCIS.Core.Application/Service/Management/UserService.cs +++ b/IRaCIS.Core.Application/Service/Management/UserService.cs @@ -277,24 +277,10 @@ namespace IRaCIS.Application.Services public async Task ResetPassword(Guid userId) { - var pwd = "123456"; + var pwd = IRCEmailPasswordHelper.GenerateRandomPassword(10); - if (_hostEnvironment.EnvironmentName != "Development") - { - pwd = "Extimaging." + new Random().Next(100, 1000); - } - - //try - //{ await _mailVerificationService.AdminResetPwdSendEmailAsync(userId, pwd); - //} - //catch (Exception) - //{ - // //---请检查邮箱地址或者联系维护人员, 邮件发送失败, 未能创建账户成功 - // throw new BusinessValidationFailedException(_localizer["User_CreateFailed"]); - //} - await _userRepository.UpdatePartialNowNoQueryAsync(userId, u => new User() { @@ -533,7 +519,7 @@ namespace IRaCIS.Application.Services } - saveItem.Password = MD5Helper.Md5("123456"); + saveItem.Password = MD5Helper.Md5(IRCEmailPasswordHelper.GenerateRandomPassword(10)); await _userRepository.AddAsync(saveItem); @@ -646,7 +632,7 @@ namespace IRaCIS.Application.Services await _mailVerificationService.SenMFAVerifyEmail(userId, userInfo.FullName, userInfo.EMail, verificationCode, (UserMFAType)mfaType); - var hiddenEmail = EmailMaskHelper.MaskEmail(userInfo.EMail); + var hiddenEmail = IRCEmailPasswordHelper.MaskEmail(userInfo.EMail); return ResponseOutput.Ok(hiddenEmail); } diff --git a/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs b/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs index b8159abca..ffef6f05a 100644 --- a/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs +++ b/IRaCIS.Core.Application/Service/SiteSurvey/TrialSiteSurveyService.cs @@ -781,6 +781,8 @@ namespace IRaCIS.Core.Application.Contracts saveItem.UserName = saveItem.UserCode; + saveItem.Password = MD5Helper.Md5(IRCEmailPasswordHelper.GenerateRandomPassword(10)); + saveItem.UserTypeEnum = _userTypeRepository.Where(t => t.Id == saveItem.UserTypeId).Select(t => t.UserTypeEnum).First(); diff --git a/IRaCIS.Core.Application/Service/TrialSiteUser/TrialExternalUserService.cs b/IRaCIS.Core.Application/Service/TrialSiteUser/TrialExternalUserService.cs index 3b1fea4d3..77462d233 100644 --- a/IRaCIS.Core.Application/Service/TrialSiteUser/TrialExternalUserService.cs +++ b/IRaCIS.Core.Application/Service/TrialSiteUser/TrialExternalUserService.cs @@ -17,6 +17,7 @@ using IRaCIS.Core.Application.Auth; using IRaCIS.Application.Services; using IRaCIS.Core.Application.Filter; using Medallion.Threading; +using IRaCIS.Core.Application.Helper; namespace IRaCIS.Core.Application.Service { @@ -131,7 +132,7 @@ namespace IRaCIS.Core.Application.Service generateUser.UserTypeEnum = _userTypeRepository.Where(t => t.Id == generateUser.UserTypeId).Select(t => t.UserTypeEnum).First(); - generateUser.Password = MD5Helper.Md5("123456"); + generateUser.Password = MD5Helper.Md5(IRCEmailPasswordHelper.GenerateRandomPassword(10)); generateUser.Status = UserStateEnum.Disable; diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index f6e407f02..19ecc3597 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -115,14 +115,11 @@ namespace IRaCIS.Application.Services } - public class TestModel2 - { - public Guid TestId { get; set; } - } + public IResponseOutput TestJson() { - return ResponseOutput.Ok(new TestModel(), new TestModel2()); + return ResponseOutput.Ok(new TestModel(), IRCEmailPasswordHelper.GenerateRandomPassword(10)); } public string TestHoliday(DateTime startdate,DateTime endDate)