From 0e300884050517db1ab3ccc6d8b0265f57beb37b Mon Sep 17 00:00:00 2001 From: lianpi3 Date: Sun, 4 Aug 2024 15:02:26 +0330 Subject: [PATCH] password hashing --- Controllers/usersController.cs | 22 ++++++++++++++++++++++ Models/Model1.edmx | 4 ++-- Views/users/Edit.cshtml | 2 +- Views/users/Index.cshtml | 12 ++---------- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Controllers/usersController.cs b/Controllers/usersController.cs index 553ed7a..f6eb5bf 100644 --- a/Controllers/usersController.cs +++ b/Controllers/usersController.cs @@ -7,6 +7,8 @@ using System.Net; using System.Web; using System.Web.Mvc; using WebApplication1.Models; +using System.Security.Cryptography; +using System.Text; namespace WebApplication1.Controllers { @@ -88,6 +90,15 @@ namespace WebApplication1.Controllers //if username is valid create user add to db else { + // Hash the password + using (var sha256 = SHA256.Create()) + { + var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(user.password)); + user.password = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); + } + + + db.users.Add(user); db.SaveChanges(); @@ -134,6 +145,11 @@ namespace WebApplication1.Controllers { //check if user exists var existingUser = db.users.FirstOrDefault(u => u.usename == user.usename ); + using (var sha256 = SHA256.Create()) + { + var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(user.password)); + user.password = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); + } if (existingUser != null && existingUser.password == user.password) { //session start @@ -182,6 +198,12 @@ namespace WebApplication1.Controllers { if (ModelState.IsValid) { + // Hash the password + using (var sha256 = SHA256.Create()) + { + var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(user.password)); + user.password = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); + } db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); diff --git a/Models/Model1.edmx b/Models/Model1.edmx index f0437e9..42082d0 100644 --- a/Models/Model1.edmx +++ b/Models/Model1.edmx @@ -65,7 +65,7 @@ - + @@ -139,7 +139,7 @@ - + diff --git a/Views/users/Edit.cshtml b/Views/users/Edit.cshtml index 4a5f2a2..f8dbbf2 100644 --- a/Views/users/Edit.cshtml +++ b/Views/users/Edit.cshtml @@ -27,7 +27,7 @@
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
- @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
diff --git a/Views/users/Index.cshtml b/Views/users/Index.cshtml index f03129e..e366fdd 100644 --- a/Views/users/Index.cshtml +++ b/Views/users/Index.cshtml @@ -15,7 +15,7 @@
- @Html.DisplayNameFor(model => model.usename) + Username
@@ -23,15 +23,7 @@
- @Html.DisplayNameFor(model => model.password) -
- -
- @Html.DisplayFor(model => model.password) -
- -
- @Html.DisplayNameFor(model => model.displayname) + Display Name