让京东的密码问题从此不再重现(NET篇)

编程入门 行业动态 更新时间:2024-10-13 04:22:52

让京东的密码问题<a href=https://www.elefans.com/category/jswz/34/1742191.html style=从此不再重现(NET篇)"/>

让京东的密码问题从此不再重现(NET篇)

2012年12月,以CSDN为首的密码泄露门第一次出现在互联网中,这样的爆发让我们这些基层程序猿们不可以理解,csdn居然没有把密码加密。哦买噶!!!转眼2016年12月,时隔4年京东电商巨头又一次遭遇了这样的悲催行为。这样的问题,就是密码简单设置导致的。

开篇结束进入正题,我是一枚NET开发者,如果通过密码安全来保护用户的隐私?这里有一个很好的解决方案,首先在项目中做一个枚举如下图:


让平台来设置不同的用户不同的加密方式(Clear可以去掉不用)。

其次,我们在写一个加密服务EncryptionService



    public class EncryptionService{        /// <summary>/// 加密/// </summary>/// <param name="password"></param>/// <param name="saltkey"></param>/// <param name="passwordFormat"></param>/// <returns></returns>public string CreatePasswordHash(string password, string saltkey,string passwordFormat = "SHA1"){if (String.IsNullOrEmpty(passwordFormat))passwordFormat = "SHA1";string saltAndPassword = String.Concat(password, saltkey);//return FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, passwordFormat);var algorithm = HashAlgorithm.Create(passwordFormat);if (algorithm == null)throw new ArgumentException("无法识别的哈希名称");var hashByteArray = algorithm.ComputeHash(Encoding.UTF8.GetBytes(saltAndPassword));return BitConverter.ToString(hashByteArray).Replace("-", "");}/// <summary>/// 加密/// </summary>/// <param name="plainText"></param>/// <param name="encryptionPrivateKey"></param>/// <returns></returns>public string EncryptText(string plainText, string encryptionPrivateKey = ""){if (string.IsNullOrEmpty(plainText))return plainText;return MD5Encrypt(plainText, new UTF8Encoding());}public string MD5Encrypt(string input, Encoding encode){MD5 md5 = new MD5CryptoServiceProvider();byte[] t = md5.ComputeHash(encode.GetBytes(input));StringBuilder sb = new StringBuilder(32);for (int i = 0; i < t.Length; i++)sb.Append(t[i].ToString("x").PadLeft(2, '0'));return sb.ToString();}}



好了,做好了这两个方法,我们来设计数据库的用户表。


因为是个教程,我没有刻意去设计很好的CodeFirst或是映射。我用的ef。大家凑合看

上图数据库设计



不用太多解释,PasswordFormatId是针对不同用户设计的不同加密方式,即便全部泄露了,解密也要很复杂。

最后在来看看控制器


    public class CustomerController : Controller{#region Login// GET: Customerpublic ActionResult Index(string tip = ""){var model = new CustomerIndexModel();ViewBag.Tip = tip;return View(model);}[HttpPost]public ActionResult Login(CustomerIndexModel model){var result = new CustomerService().Login(model.LoginName);if(result == null)return RedirectToAction("Index", new { tip = "该账户不存在" });bool Verification = false;switch ((PasswordFormat)result.PasswordFormatId){case PasswordFormat.Clear:Verification = model.LoginPassword == result.Password;break;case PasswordFormat.Encrypted:Verification = new EncryptionService().EncryptText(model.LoginPassword, result.PasswordSalt) == result.Password;break;case PasswordFormat.Hashed:Verification = new EncryptionService().CreatePasswordHash(model.LoginPassword, result.PasswordSalt) == result.Password;break;default:Verification = new EncryptionService().CreatePasswordHash(model.LoginPassword, result.PasswordSalt) == result.Password;break;}if (!Verification)return RedirectToAction("Index", new { tip = "密码错误" });elsereturn RedirectToAction("Index", new { tip = "登录成功" });}#endregion#region Registerpublic ActionResult Register(){var model = new CustomerRegisterModel();return View(model);}[HttpPost]public ActionResult Register(CustomerRegisterModel model){//判定是否录入验证通过if (ModelState.IsValid){var entity = new Models.Customer {Active = true,Deleted = false,Email = model.Email,LoginName = model.LoginName,Password = model.LoginPassword,Mobile = model.Mobile,//假设加密方式为PasswordFormatId = (int)PasswordFormat.Hashed};new CustomerService().InsertCusotmer(entity);return RedirectToAction("Index", new { tip ="注册成功" });}return View();}#endregion}资源下载地址:

这个不用过多解释。

大家如果有什么疑问请加群 68848430 咨询。

我会把资源上传上来供大家学习

更多推荐

让京东的密码问题从此不再重现(NET篇)

本文发布于:2024-02-25 19:19:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1700038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:从此不再   京东   密码   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!