public ActionResult EditPassword(Entities.ModifyModel modela, string returnUrl = null) { ModelState.Remove("Id"); ViewBag.ReturnUrl = Url.IsLocalUrl(returnUrl) ? returnUrl : Url.RouteUrl("mainIndex"); if (!ModelState.IsValid) { return(View(modela)); } var model = _sysUserService.getById(WorkContext.CurrentUser.Id); if (model.Password == EncryptorHelper.GetMD5(modela.OriginalPassword.Trim() + model.Salt)) { if (modela.ConfirmedPassword == modela.ModifiedPassword) { model.Password = EncryptorHelper.GetMD5(modela.ConfirmedPassword.Trim() + model.Salt); //model.Name.Trim();; //model.Modifier = WorkContext.CurrentUser.Id; _sysUserService.updatePassword(model); } else { return(Redirect(Url.IsLocalUrl(returnUrl) ? returnUrl : Url.RouteUrl("password"))); } } else { return(Redirect(Url.IsLocalUrl(returnUrl) ? returnUrl : Url.RouteUrl("password"))); } return(Redirect(ViewBag.ReturnUrl)); }
public ActionResult EditUser(Entities.SysUser model, string returnUrl = null) { ModelState.Remove("Id"); ViewBag.ReturnUrl = Url.IsLocalUrl(returnUrl) ? returnUrl : Url.RouteUrl("userIndex"); if (!ModelState.IsValid) { return(View(model)); } if (!String.IsNullOrEmpty(model.MobilePhone)) { model.MobilePhone = StringUitls.toDBC(model.MobilePhone); } model.Name = model.Name.Trim(); if (model.Id == Guid.Empty) { model.Id = Guid.NewGuid(); model.CreationTime = DateTime.Now; model.Salt = EncryptorHelper.CreateSaltKey(); model.Account = StringUitls.toDBC(model.Account.Trim()); model.Enabled = true; model.IsAdmin = false; model.Password = EncryptorHelper.GetMD5(model.Account + model.Salt); model.Creator = WorkContext.CurrentUser.Id; _sysUserService.insertSysUser(model); } else { model.ModifiedTime = DateTime.Now; model.Modifier = WorkContext.CurrentUser.Id; _sysUserService.updateSysUser(model); } return(Redirect(ViewBag.ReturnUrl)); }
public IActionResult OnPost() { if (!ModelState.IsValid) { return(NotValid()); } (bool Status, string Message)res; var item = _mapper.Map <Entities.Sys_User>(SysUser); if (SysUser.Id != Guid.Empty) { res = _sysUserService.UpdateUser(SysUser, UserId); } else { item.Account = item.Account.TrimSpace(); item.Id = CombGuid.NewGuid(); item.CreationTime = DateTime.Now; item.Creator = UserId; item.Salt = EncryptorHelper.CreateSaltKey(); item.Password = (EncryptorHelper.GetMD5(item.Account + item.Salt)); res = _sysUserService.AddUser(item); } AjaxData.Message = res.Message; AjaxData.Code = res.Status ? 0 : 2001; if (res.Status) { _sysRoleService.SetUserRoles(item.Id, RoleIds, UserId); } return(Json(AjaxData)); }
private void ShouldReturnPublicKey() { var keyPair = EncryptorHelper.GenerateKeyPair(curve, algorithm); var publicKey = EncryptorHelper.GetPublicKey(keyPair); publicKey.Should().NotBeNull(); }
public static AccountAttributeEntity ConvertAttribute(string encryptKey, AccountAttribute attribute) { AccountAttributeEntity tmpAttributeEntity = null; if (attribute != null) { tmpAttributeEntity = new AccountAttributeEntity(); tmpAttributeEntity.AttributeId = attribute.AttributeId; tmpAttributeEntity.Order = attribute.Order; tmpAttributeEntity.Name = attribute.Name; tmpAttributeEntity.AccountId = attribute.AccountId; tmpAttributeEntity.Encrypted = attribute.Encrypted; if (!tmpAttributeEntity.Encrypted) { tmpAttributeEntity.Value = attribute.Value; } else { tmpAttributeEntity.Value = EncryptorHelper.DESEncrypt(encryptKey, attribute.Value); } } return(tmpAttributeEntity); }
public static AccountModel Convert(string encryptKey, AccountEntity entity) { AccountModel tmpAccountModel = null; if (entity != null) { tmpAccountModel = new AccountModel(); tmpAccountModel.AccountId = entity.AccountId; tmpAccountModel.AccountGuid = entity.AccountGuid; tmpAccountModel.CatalogId = entity.CatalogId; tmpAccountModel.Name = entity.Name; tmpAccountModel.URL = entity.URL; tmpAccountModel.Order = entity.Order; tmpAccountModel.TopMost = entity.TopMost; tmpAccountModel.Deleted = entity.Deleted; tmpAccountModel.VersionNo = entity.VersionNo; if (System.Enum.IsDefined(typeof(SecretRank), entity.SecretRank)) { tmpAccountModel.SecretRank = (SecretRank)entity.SecretRank; } tmpAccountModel.Email = EncryptorHelper.DESDecrypt(encryptKey, entity.Email); tmpAccountModel.Mobile = EncryptorHelper.DESDecrypt(encryptKey, entity.Mobile); tmpAccountModel.LoginName = EncryptorHelper.DESDecrypt(encryptKey, entity.LoginName); tmpAccountModel.Password = EncryptorHelper.DESDecrypt(encryptKey, entity.Password); tmpAccountModel.CreateTime = entity.CreateTime; tmpAccountModel.UpdateTime = entity.UpdateTime; tmpAccountModel.Comment = entity.Comment; } return(tmpAccountModel); }
public static AccountEntity Convert(string encryptKey, AccountModel account) { AccountEntity tmpAccountEntity = null; if (account != null) { tmpAccountEntity = new AccountEntity(); tmpAccountEntity.AccountId = account.AccountId; tmpAccountEntity.AccountGuid = account.AccountGuid; tmpAccountEntity.CatalogId = account.CatalogId; tmpAccountEntity.Name = account.Name; tmpAccountEntity.URL = account.URL; tmpAccountEntity.Order = account.Order; tmpAccountEntity.TopMost = account.TopMost; tmpAccountEntity.Deleted = account.Deleted; tmpAccountEntity.VersionNo = account.VersionNo; tmpAccountEntity.SecretRank = (ushort)account.SecretRank; tmpAccountEntity.Email = EncryptorHelper.DESEncrypt(encryptKey, account.Email); tmpAccountEntity.Mobile = EncryptorHelper.DESEncrypt(encryptKey, account.Mobile); tmpAccountEntity.LoginName = EncryptorHelper.DESEncrypt(encryptKey, account.LoginName); tmpAccountEntity.Password = EncryptorHelper.DESEncrypt(encryptKey, account.Password); tmpAccountEntity.CreateTime = account.CreateTime; tmpAccountEntity.UpdateTime = account.UpdateTime; tmpAccountEntity.Comment = account.Comment; } return(tmpAccountEntity); }
public IActionResult UserEdit(Sys_UserMapping SysUser, List <string> RoleIds) { (bool Status, string Message)res; var item = _mapper.Map <Entities.sys_user>(SysUser); if (!String.IsNullOrEmpty(SysUser.id)) { res = _sysUserService.UpdateUser(SysUser, UserId); } else { item.account = item.account.TrimSpace(); item.id = CombGuid.NewGuidAsString(); item.creation_time = DateTime.Now; item.creator = UserId; item.salt = EncryptorHelper.CreateSaltKey(); item.password = (EncryptorHelper.GetMD5(item.account + item.salt)); res = _sysUserService.AddUser(item); } AjaxData.Message = res.Message; AjaxData.Success = res.Status; if (res.Status) { _sysRoleService.SetUserRoles(item.id, RoleIds, UserId); } return(Json(AjaxData)); }
public static AccountAttribute ConvertAttribute(string encryptKey, AccountAttributeEntity entity) { AccountAttribute tmpAttribute = null; if (entity != null) { tmpAttribute = new AccountAttribute(); tmpAttribute.AttributeId = entity.AttributeId; tmpAttribute.Order = entity.Order; tmpAttribute.Name = entity.Name; tmpAttribute.AccountId = entity.AccountId; tmpAttribute.Encrypted = entity.Encrypted; if (!tmpAttribute.Encrypted) { tmpAttribute.Value = entity.Value; } else { tmpAttribute.Value = EncryptorHelper.DESDecrypt(encryptKey, entity.Value); } } return(tmpAttribute); }
/// <summary> /// 验证签名 /// </summary> /// <param name="signature"></param> /// <param name="contentMD5"></param> /// <param name="VERB"></param> /// <returns></returns> private bool ValidSignature(string signature, string contentMD5, string VERB) { var settings = _settingService.GetMasterSettings(); string signatureString = EncryptorHelper.HmacSha1(settings.OSSAccessKeySecret, $"{VERB}{contentMD5}"); return(signatureString.Equals(signature, StringComparison.InvariantCultureIgnoreCase)); }
private bool CreateApplicationConfig(string configDirectory, string userName, string password) { var tmpSafePassConfig = new SafePassConfiguration(); if (System.Globalization.CultureInfo.CurrentCulture.Name == "zh-CN") { tmpSafePassConfig.Application.LanguageFile = ApplicationDefines.ChineseSimpLanguageFile; } var tmpSecurityProfile = tmpSafePassConfig.Application.Security; tmpSecurityProfile.LockWorkspace = Program.Config.Application.Security.LockWorkspace; tmpSecurityProfile.MasterPassword = Program.Config.Application.Security.MasterPassword; tmpSecurityProfile.CurrentAccount.UserName = userName; tmpSecurityProfile.CurrentAccount.Password = password; tmpSecurityProfile.CurrentAccount.PasswordStored = EncryptorHelper.DESEncrypt(Account.CurrentAccount.SecretKey, tmpSecurityProfile.CurrentAccount.PasswordMd5); tmpSecurityProfile.Clipboard.ClipboardClearOnExit = Program.Config.Application.Security.Clipboard.ClipboardClearOnExit; tmpSecurityProfile.Clipboard.ClipboardClearAfterSeconds = Program.Config.Application.Security.Clipboard.ClipboardClearAfterSeconds; tmpSecurityProfile.SecretRank.SecretRank0Color = Program.Config.Application.Security.SecretRank.SecretRank0Color; tmpSecurityProfile.SecretRank.SecretRank1Color = Program.Config.Application.Security.SecretRank.SecretRank1Color; tmpSecurityProfile.SecretRank.SecretRank2Color = Program.Config.Application.Security.SecretRank.SecretRank2Color; tmpSecurityProfile.SecretRank.SecretRank3Color = Program.Config.Application.Security.SecretRank.SecretRank3Color; tmpSafePassConfig.MainWindow = Program.Config.MainWindow; var tmpCreateResult = ApplicationConfigSerializer.SaveApplicationConfig(Path.Combine(configDirectory, "SafePass.config.xml"), tmpSafePassConfig); return(tmpCreateResult); }
private void ShouldGenerate32ByteRandomKey() { var randomKey = EncryptorHelper.GenerateRandomKey(); var randomKeyByte = GetByteFromBase64(randomKey); randomKeyByte.Count().Should().Be(32); }
public IActionResult Signature([FromBody] SignatureModel model) { if (!ModelState.IsValid) { ApiData.code = 1005; ApiData.msg = ModelState.GetErrMsg(); return(Ok(ApiData)); } var settings = _settingService.GetMasterSettings(); if (String.IsNullOrEmpty(settings.OSSAccessKeyId) || String.IsNullOrEmpty(settings.OSSAccessKeyId)) { ApiData.code = 2001; ApiData.msg = "暂未开放上传操作"; return(Ok(ApiData)); } if (!settings.OSSAccessKeyId.Equals(model.AccessKeyId, StringComparison.InvariantCultureIgnoreCase)) { ApiData.code = 2001; ApiData.msg = "AccessKeyId错误"; return(Ok(ApiData)); } var signatureString = EncryptorHelper.HmacSha1(settings.OSSAccessKeySecret, $"{model.VERB}{model.ContentMD5}"); ApiData.code = 0; ApiData.msg = "获取成功"; ApiData.data = new { Signature = signatureString }; return(Ok(ApiData)); }
/// <summary> /// 修改密码,重置密码 /// </summary> /// <param name="userId"></param> /// <param name="oldPwd"></param> /// <param name="newPwd"></param> /// <param name="modifier"></param> /// <param name="reset">重置密码,只有管理员的操作</param> /// <returns></returns> public (bool Status, string Message) UpdatePwd(Guid userId, string oldPwd, string newPwd, Guid modifier, bool reset = false) { var user = _dbContext.Sys_User.Find(userId); if (user == null) { return(false, "用户不存在"); } string oldJson = JsonConvert.SerializeObject(user); if (reset) { user.Password = EncryptorHelper.GetMD5(user.Account + user.Salt); } else { if (user.Password.Equals(oldPwd, StringComparison.InvariantCultureIgnoreCase)) { user.Password = newPwd; } else { return(false, "原密码错误"); } } _dbContext.SaveChanges(); string newJson = JsonConvert.SerializeObject(user); _activityLogService.InsertedEntity <Entities.Sys_User>(userId, oldJson, newJson, modifier); return(true, "修改成功"); }
private void ShouldGenerateBase64String() { const string testString = "dGhpcyBpcyBhIHN0cmluZwo="; var testStringByte = EncryptorHelper.GetByteFromBase64(testString); EncryptorHelper.GetBase64FromByte(testStringByte).Should().Be(testString); }
/// <summary> /// 用户自己修改密码 /// </summary> /// <param name="id"></param> /// <param name="password"></param> public void changePassword(Guid id, string password) { var sysUser = _sysUserRepository.getById(id); sysUser.Password = EncryptorHelper.GetMD5(password + sysUser.Salt); sysUser.Modifier = sysUser.Id; _sysUserRepository.update(sysUser); }
/// <summary> /// 重置密码。默认重置成账号一样 /// </summary> /// <param name="id"></param> /// <param name="modifer"></param> public void resetPassword(Guid id, Guid modifer) { var sysUser = _sysUserRepository.getById(id); sysUser.Password = EncryptorHelper.GetMD5(sysUser.Account + sysUser.Salt); sysUser.Modifier = modifer; _sysUserRepository.update(sysUser); }
public void changePassword(Guid id, string password) { var user = _sysUserRepository.getById(id); var mdPassword = EncryptorHelper.GetMD5(password + user.Salt); user.Password = mdPassword; _sysUserRepository.update(user); }
private void buttonOK_Click(object sender, System.EventArgs args) { var tmpInputErrorCaption = SafePassResource.MessageBoxCaptionInputError; var tmpOldPassword = this.textOldPassword.Text.Trim(); var tmpNewPassword = this.textNewPassword.Text.Trim(); if (tmpOldPassword != Account.CurrentAccount.Password) { this.textOldPassword.Focus(); MessageBox.Show(SafePassResource.ChangePasswordWindowPromptPasswordIncorrect, tmpInputErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(tmpNewPassword)) { this.textNewPassword.Focus(); MessageBox.Show(SafePassResource.ChangePasswordWindowPromptPasswordIsEmpty, tmpInputErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else if (!string.Equals(tmpNewPassword, this.textRepeatPassword.Text)) { this.textRepeatPassword.Focus(); MessageBox.Show(SafePassResource.PasswordRepeatFailed, tmpInputErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.Equals(tmpOldPassword, tmpNewPassword, System.StringComparison.OrdinalIgnoreCase)) { this.textNewPassword.Focus(); MessageBox.Show(SafePassResource.ChangePasswordWindowPromptSameAsOldPassword, tmpInputErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { var tmpPasswordMd5 = Md5DigestHelper.Md5Salt(tmpNewPassword, Account.CurrentAccount.UserName); var tmpAccountService = new HuiruiSoft.Safe.Service.AccountService(); var tmpChangeResult = tmpAccountService.ChangePassword(tmpPasswordMd5); if (tmpChangeResult) { DataBaseConfig.Password = tmpPasswordMd5; Account.CurrentAccount.Password = tmpNewPassword; Account.CurrentAccount.PasswordStored = EncryptorHelper.DESEncrypt(Account.CurrentAccount.SecretKey, tmpPasswordMd5); ApplicationConfigSerializer.SaveApplicationConfig(Program.Config); MessageBox.Show(SafePassResource.ChangePasswordWindowMessageChangeSuccess, SafePassResource.Success, MessageBoxButtons.OK, MessageBoxIcon.Information); this.DialogResult = DialogResult.OK; } } catch (System.SystemException exception) { loger.Error(exception); MessageBox.Show(SafePassResource.ChangePasswordWindowMessageChangeFailed, SafePassResource.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ShouldGenerate32ByteXor() { var randomKey1 = EncryptorHelper.GenerateRandomKey(); var randomKey2 = EncryptorHelper.GenerateRandomKey(); var xorRandom = EncryptorHelper.XorOfRandom(randomKey1, randomKey2); xorRandom.Count().Should().Be(32); }
/// <summary> /// 重置密码。默认重置成账号一样 /// </summary> /// <param name="id"></param> /// <param name="modifer"></param> public void resetPassword(Guid id, Guid modifer) { var user = _sysUserRepository.getById(id); user.Password = EncryptorHelper.GetMD5(user.Account.Trim() + user.Salt); user.Modifier = modifer; user.ModifiedTime = DateTime.Now; _sysUserRepository.update(user); }
public IActionResult LoginIndex() { string r = EncryptorHelper.GetMD5(Guid.NewGuid().ToString()); HttpContext.Session.SetString(Login_Key, r); LoginModel loginModel = new LoginModel { R = r }; return(View(loginModel)); }
public ActionResult ResetPassword(Guid id) { var modelpass = _sysUserService.getById(id); modelpass.Password = EncryptorHelper.GetMD5("Sacc2020" + modelpass.Salt); modelpass.Modifier = WorkContext.CurrentUser.Id; _sysUserService.resetPassword(modelpass); AjaxData.Status = true; AjaxData.Message = "用户密码已重置为原始密码"; // return Json(AjaxData); return(Redirect(Url.IsLocalUrl(null) ? null : Url.RouteUrl("userIndex"))); }
/// <summary> /// 修改密码 /// </summary> /// <param name="id">用户id</param> /// <param name="password">新密码</param> public void ChangePassword(Guid id, string password) { var user = _sysUserRepository.GetById(id); if (user != null) { user.Password = EncryptorHelper.GetMD5(password + user.Salt); user.ModifiedTime = DateTime.Now; user.Modifier = user.Id; _sysUserRepository.DbContext.SaveChanges(); } }
public static void InitData(this IApplicationBuilder app) { #region 自动创建数据库 // Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.SqlServer.Design //dotnet ef migrations add InitialEFDbContext -c EFDbContext -o Data/Migrations/DemoDB using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope()) { var context = serviceScope.ServiceProvider.GetRequiredService <EFDbContext>(); /* * System.Data.SqlClient.SqlException:“将 FOREIGN KEY 约束 'FK_SysPermission_SysRole_RoleId' 引入表 'SysPermission' * 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 * 无法创建约束。请参阅前面的错误消息。 */ context.Database.Migrate(); } #endregion #region 初始化数据 var _sysUserService = EnginContext.Current.Resolve <ISysUserService>(); var _sysRoleService = EnginContext.Current.Resolve <ISysRoleService>(); SysUser sysUser = new SysUser(); if (!_sysUserService.ExistUser()) { sysUser.Id = Guid.NewGuid(); sysUser.Account = "admin"; sysUser.Name = "超级管理员"; sysUser.Email = ""; sysUser.MobilePhone = ""; sysUser.Salt = EncryptorHelper.CreateSaltKey(); sysUser.Password = EncryptorHelper.GetMD5(sysUser.Account + sysUser.Salt); sysUser.Enabled = true; sysUser.IsAdmin = true; sysUser.CreationTime = DateTime.Now; sysUser.LoginLock = false; sysUser.IsDeleted = false; _sysUserService.InsertSysUser(sysUser); } if (!_sysRoleService.ExistRole()) { SysRole sysRole = new SysRole() { Id = Guid.NewGuid(), Name = "超级管理员", Creator = sysUser.Id, CreationTime = DateTime.Now }; _sysRoleService.InsertRole(sysRole); } #endregion }
public IActionResult Login() { var r = EncryptorHelper.GetMD5(Guid.NewGuid().ToString()); HttpContext.Session.SetString(S_KEY, r); string ramdom = HttpContext.Session.GetString(S_KEY); LoginModel model = new LoginModel() { R = r }; return(View(model)); }
public IActionResult Index() { string s = EncryptorHelper.GetMd5(Guid.NewGuid().ToString()); HttpContext.Session.SetString(R_KEY, s); //HttpContext.Response.Cookies.Append(R_KEY,s); LoginModel loginModel = new LoginModel() { Range = s }; return(View(loginModel)); }
public IActionResult ChangePassword(string password) { if (_adminAuthService.getCurrentUser() == null) { Redirect(Url.RouteUrl("publicLogin")); } var user = _adminAuthService.getCurrentUser(); user.Password = EncryptorHelper.GetMD5(password + user.Salt); _sysUserService.updateSysUser(user); return(Json(new { status = true, Message = "密码修改成功!" })); }
/// <summary> /// 用户登陆验证 /// </summary> /// <param name="account"></param> /// <param name="password"></param> /// <param name="platform">0:web,1:app</param> /// <returns></returns> public (bool Status, string Message, Entities.Sys_User User, Entities.Sys_UserJwt Jwt) ValidateUser(string account, string password, int platform = 0) { var user = _dbContext.Sys_User.Where(o => o.Account == account && !o.IsDeleted).FirstOrDefault(); if (user == null) { return(false, "账号或密码错误", null, null); } var r_item = _dbContext.Sys_UserR.FirstOrDefault(o => o.UserId == user.Id && o.Platform == platform); if (r_item == null) { return(false, "非法操作,因子不存在,请重试", null, null); } var pwd = EncryptorHelper.GetMD5((user.Password ?? "") + r_item.R); var log = new Sys_UserLogin() { Id = CombGuid.NewGuid(), UserId = user.Id, IpAddress = _webHelper.GetIPAddress(), LoginTime = DateTime.Now, Status = false }; Entities.Sys_UserJwt jwt = null; string msg = "账号或密码错误"; if (password.Equals(pwd, StringComparison.InvariantCultureIgnoreCase)) { log.Status = true; msg = "登陆成功"; user.LastIpAddress = log.IpAddress; _dbContext.Sys_UserR.Remove(r_item); jwt = new Sys_UserJwt() { Jti = EncryptorHelper.GetMD5(Guid.NewGuid().ToString()), Expiration = DateTime.Now.AddDays(30), RefreshToken = EncryptorHelper.GetMD5(Guid.NewGuid().ToString()), Platform = platform, UserId = user.Id }; _dbContext.Sys_UserJwt.Add(jwt); } _dbContext.Sys_UserLogin.Add(log); _dbContext.SaveChanges(); return(log.Status, msg, user, jwt); }
public pl_user_info getUserInfoByName(string userName, string passwordDecrypted) { DALLogin dal = new DALLogin(); string passwordEncrypted = EncryptorHelper.Encryptor(passwordDecrypted); IList <pl_user_info> userInfo = dal.getUserInfoByName(userName, passwordEncrypted); if (userInfo.Count > 0) { return(userInfo[0]); } else { return(null); } }