password hashing

This commit is contained in:
2024-08-04 15:02:26 +03:30
parent be864f564b
commit 0e30088405
4 changed files with 27 additions and 13 deletions

View File

@ -7,6 +7,8 @@ using System.Net;
using System.Web; using System.Web;
using System.Web.Mvc; using System.Web.Mvc;
using WebApplication1.Models; using WebApplication1.Models;
using System.Security.Cryptography;
using System.Text;
namespace WebApplication1.Controllers namespace WebApplication1.Controllers
{ {
@ -88,6 +90,15 @@ namespace WebApplication1.Controllers
//if username is valid create user add to db //if username is valid create user add to db
else 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.users.Add(user);
db.SaveChanges(); db.SaveChanges();
@ -134,6 +145,11 @@ namespace WebApplication1.Controllers
{ {
//check if user exists //check if user exists
var existingUser = db.users.FirstOrDefault(u => u.usename == user.usename ); 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) if (existingUser != null && existingUser.password == user.password)
{ {
//session start //session start
@ -182,6 +198,12 @@ namespace WebApplication1.Controllers
{ {
if (ModelState.IsValid) 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.Entry(user).State = EntityState.Modified;
db.SaveChanges(); db.SaveChanges();
return RedirectToAction("Index"); return RedirectToAction("Index");

View File

@ -65,7 +65,7 @@
</Key> </Key>
<Property Name="ID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" /> <Property Name="ID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="usename" Type="nvarchar" MaxLength="50" Nullable="false" /> <Property Name="usename" Type="nvarchar" MaxLength="50" Nullable="false" />
<Property Name="password" Type="nvarchar" MaxLength="50" Nullable="false" /> <Property Name="password" Type="nvarchar(max)" Nullable="false" />
<Property Name="displayname" Type="nvarchar" MaxLength="50" /> <Property Name="displayname" Type="nvarchar" MaxLength="50" />
<Property Name="role" Type="int" Nullable="false" /> <Property Name="role" Type="int" Nullable="false" />
</EntityType> </EntityType>
@ -139,7 +139,7 @@
</Key> </Key>
<Property Name="ID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> <Property Name="ID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Name="usename" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="true" /> <Property Name="usename" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="true" />
<Property Name="password" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="true" /> <Property Name="password" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="displayname" Type="String" MaxLength="50" FixedLength="false" Unicode="true" /> <Property Name="displayname" Type="String" MaxLength="50" FixedLength="false" Unicode="true" />
<Property Name="role" Type="Int32" Nullable="false" /> <Property Name="role" Type="Int32" Nullable="false" />
</EntityType> </EntityType>

View File

@ -27,7 +27,7 @@
<div class="form-group"> <div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10"> <div class="col-md-10">
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } }) <input type="password", name="password" , class="form-control"/>
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" }) @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div> </div>
</div> </div>

View File

@ -15,7 +15,7 @@
<hr /> <hr />
<dl class="dl-horizontal"> <dl class="dl-horizontal">
<dt> <dt>
@Html.DisplayNameFor(model => model.usename) Username
</dt> </dt>
<dd> <dd>
@ -23,15 +23,7 @@
</dd> </dd>
<dt> <dt>
@Html.DisplayNameFor(model => model.password) Display Name
</dt>
<dd>
@Html.DisplayFor(model => model.password)
</dd>
<dt>
@Html.DisplayNameFor(model => model.displayname)
</dt> </dt>
<dd> <dd>