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.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");

View File

@ -65,7 +65,7 @@
</Key>
<Property Name="ID" Type="int" StoreGeneratedPattern="Identity" 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="role" Type="int" Nullable="false" />
</EntityType>
@ -139,7 +139,7 @@
</Key>
<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="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="role" Type="Int32" Nullable="false" />
</EntityType>

View File

@ -27,7 +27,7 @@
<div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<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" })
</div>
</div>

View File

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