Authenticate is jó a passwordhas-sel

This commit is contained in:
2025-01-21 16:20:55 +01:00
parent 30d82fc9bf
commit 177aa668a6
4 changed files with 20 additions and 9 deletions
@@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq; using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -10,14 +12,12 @@ namespace WorkFlowCheck.Common.Security
public static class PasswordHasher public static class PasswordHasher
{ {
// Jelszó hashelése // Jelszó hashelése
private const string _saltstring = "xc45DDfrt!!ED210:";
public static string HashPassword(string password) public static string HashPassword(string password)
{ {
// Generate a random salt // Generate a random salt
byte[] salt = new byte[16]; byte[] salt = Encoding.UTF8.GetBytes(_saltstring);
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(salt);
}
// Hash the password with the salt // Hash the password with the salt
using (var rfc2898 = new Rfc2898DeriveBytes(password, salt, 10000, HashAlgorithmName.SHA256)) using (var rfc2898 = new Rfc2898DeriveBytes(password, salt, 10000, HashAlgorithmName.SHA256))
@@ -37,7 +37,7 @@ namespace WorkFlowCheck.Common.Security
byte[] hashBytes = Convert.FromBase64String(storedPasswordHash); byte[] hashBytes = Convert.FromBase64String(storedPasswordHash);
// Extract salt (first 16 bytes) and stored hash (next 32 bytes) // Extract salt (first 16 bytes) and stored hash (next 32 bytes)
byte[] salt = new byte[16]; byte[] salt = Encoding.UTF8.GetBytes(_saltstring);
byte[] storedHash = new byte[32]; byte[] storedHash = new byte[32];
Array.Copy(hashBytes, 0, salt, 0, 16); Array.Copy(hashBytes, 0, salt, 0, 16);
Array.Copy(hashBytes, 16, storedHash, 0, 32); Array.Copy(hashBytes, 16, storedHash, 0, 32);
+1 -1
View File
@@ -9,6 +9,6 @@ namespace WorkFlowCheck.DL.Entities
public string FirstName { get; set; } = null!; public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!; public string LastName { get; set; } = null!;
public string UserName { get; set; } = null!; public string UserName { get; set; } = null!;
public string PasswordHash { get; set; } public string? PasswordHash { get; set; } = null;
} }
} }
+9 -2
View File
@@ -1,8 +1,10 @@
using System; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using WorkFlowCheck.Common.Security;
namespace WorkFlowCheck.DL.Seed namespace WorkFlowCheck.DL.Seed
{ {
@@ -10,7 +12,12 @@ namespace WorkFlowCheck.DL.Seed
{ {
public static void Seed(AppDbContext dbContext) public static void Seed(AppDbContext dbContext)
{ {
var user = dbContext.Users.Where(w => w.Email == "info@wfcapi.hu").FirstOrDefault();
if (user != null && user.PasswordHash == null)
{
user.PasswordHash = PasswordHasher.HashPassword("861354");
dbContext.SaveChanges();
}
} }
} }
} }
@@ -15,4 +15,8 @@
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WorkFlowCheck.Common\WorkFlowCheck.Common.csproj" />
</ItemGroup>
</Project> </Project>