Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
HIAST-Clinics
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
almohanad.hafez
HIAST-Clinics
Commits
45a07430
Commit
45a07430
authored
Aug 20, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Add password hasher, seed user roles (and fix id bug in role)
parent
00408ab3
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
99 additions
and
10 deletions
+99
-10
SeedHelper.cs
Clinics.Backend/API/SeedDatabaseHelper/SeedHelper.cs
+5
-1
RoleConfiguration.cs
...stence/Configurations/Identity/Roles/RoleConfiguration.cs
+2
-0
IPasswordHasher.cs
.../Persistence/Identity/PasswordsHashing/IPasswordHasher.cs
+8
-0
PasswordHasher.cs
...d/Persistence/Identity/PasswordsHashing/PasswordHasher.cs
+46
-0
20240820212742_Add_Identity.Designer.cs
...stence/Migrations/20240820212742_Add_Identity.Designer.cs
+1
-4
20240820212742_Add_Identity.cs
...end/Persistence/Migrations/20240820212742_Add_Identity.cs
+1
-2
ClinicsDbContextModelSnapshot.cs
...d/Persistence/Migrations/ClinicsDbContextModelSnapshot.cs
+0
-3
UserRoles.cs
Clinics.Backend/Persistence/SeedDatabase/UserRoles.cs
+36
-0
No files found.
Clinics.Backend/API/SeedDatabaseHelper/SeedHelper.cs
View file @
45a07430
using
Domain.Entities.Medicals.Medicines.MedicineFormValues
;
using
Domain.Entities.Identity.UserRoles
;
using
Domain.Entities.Medicals.Medicines.MedicineFormValues
;
using
Domain.Entities.People.Doctors.Shared.Constants.DoctorStatusValues
;
using
Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers.FamilyRoleValues
;
using
Domain.Entities.People.Shared.GenderValues
;
...
...
@@ -23,6 +24,9 @@ public class SeedHelper
var
seedMedicineForms
=
serviceScope
.
ServiceProvider
.
GetRequiredService
<
ISeed
<
MedicineForm
>>();
await
seedMedicineForms
.
Seed
();
var
seedUserRoles
=
serviceScope
.
ServiceProvider
.
GetRequiredService
<
ISeed
<
Role
>>();
await
seedUserRoles
.
Seed
();
}
}
}
Clinics.Backend/Persistence/Configurations/Identity/Roles/RoleConfiguration.cs
View file @
45a07430
...
...
@@ -10,6 +10,8 @@ public class RoleConfiguration : IEntityTypeConfiguration<Role>
{
builder
.
ToTable
(
nameof
(
Role
));
builder
.
Property
(
role
=>
role
.
Id
).
ValueGeneratedNever
();
builder
.
Property
(
role
=>
role
.
Name
)
.
HasMaxLength
(
50
);
...
...
Clinics.Backend/Persistence/Identity/PasswordsHashing/IPasswordHasher.cs
0 → 100644
View file @
45a07430
namespace
Persistence.Identity.PasswordsHashing
;
public
interface
IPasswordHasher
{
public
string
Hash
(
string
password
);
public
bool
Verify
(
string
password
,
string
passwordHash
);
}
Clinics.Backend/Persistence/Identity/PasswordsHashing/PasswordHasher.cs
0 → 100644
View file @
45a07430
using
System.Security.Cryptography
;
namespace
Persistence.Identity.PasswordsHashing
;
public
class
PasswordHasher
:
IPasswordHasher
{
#
region
Hashing
// Salting is adding a random number to the hash
// to ensure that users with same passwords have
// different hash values.
// Best pactice is to use 128 bits = 16 bytes
private
const
int
SaltSize
=
16
;
// Output size
// Best practice is to use 256 bits
private
const
int
HashSize
=
32
;
private
const
int
Iterations
=
100000
;
private
readonly
HashAlgorithmName
Algorithm
=
HashAlgorithmName
.
SHA512
;
public
string
Hash
(
string
password
)
{
byte
[]
salt
=
RandomNumberGenerator
.
GetBytes
(
SaltSize
);
byte
[]
hash
=
Rfc2898DeriveBytes
.
Pbkdf2
(
password
,
salt
,
Iterations
,
Algorithm
,
HashSize
);
// Pdkdf ~ Password Based Key Derivation Function
return
$"
{
Convert
.
ToHexString
(
hash
)}
-
{
Convert
.
ToHexString
(
salt
)}
"
;
}
#
endregion
#
region
Verification
public
bool
Verify
(
string
password
,
string
passwordHash
)
{
string
[]
parts
=
passwordHash
.
Split
(
'-'
);
byte
[]
hash
=
Convert
.
FromHexString
(
parts
[
0
]);
byte
[]
salt
=
Convert
.
FromHexString
(
parts
[
1
]);
byte
[]
inputHash
=
Rfc2898DeriveBytes
.
Pbkdf2
(
password
,
salt
,
Iterations
,
Algorithm
,
HashSize
);
return
CryptographicOperations
.
FixedTimeEquals
(
hash
,
inputHash
);
}
#
endregion
}
Clinics.Backend/Persistence/Migrations/202408202
02107
_Add_Identity.Designer.cs
→
Clinics.Backend/Persistence/Migrations/202408202
12742
_Add_Identity.Designer.cs
View file @
45a07430
...
...
@@ -12,7 +12,7 @@ using Persistence.Context;
namespace
Persistence.Migrations
{
[
DbContext
(
typeof
(
ClinicsDbContext
))]
[
Migration
(
"202408202
02107
_Add_Identity"
)]
[
Migration
(
"202408202
12742
_Add_Identity"
)]
partial
class
Add_Identity
{
/// <inheritdoc />
...
...
@@ -28,11 +28,8 @@ namespace Persistence.Migrations
modelBuilder
.
Entity
(
"Domain.Entities.Identity.UserRoles.Role"
,
b
=>
{
b
.
Property
<
int
>(
"Id"
)
.
ValueGeneratedOnAdd
()
.
HasColumnType
(
"int"
);
SqlServerPropertyBuilderExtensions
.
UseIdentityColumn
(
b
.
Property
<
int
>(
"Id"
));
b
.
Property
<
string
>(
"Name"
)
.
IsRequired
()
.
HasMaxLength
(
50
)
...
...
Clinics.Backend/Persistence/Migrations/202408202
02107
_Add_Identity.cs
→
Clinics.Backend/Persistence/Migrations/202408202
12742
_Add_Identity.cs
View file @
45a07430
...
...
@@ -14,8 +14,7 @@ namespace Persistence.Migrations
name
:
"Role"
,
columns
:
table
=>
new
{
Id
=
table
.
Column
<
int
>(
type
:
"int"
,
nullable
:
false
)
.
Annotation
(
"SqlServer:Identity"
,
"1, 1"
),
Id
=
table
.
Column
<
int
>(
type
:
"int"
,
nullable
:
false
),
Name
=
table
.
Column
<
string
>(
type
:
"nvarchar(50)"
,
maxLength
:
50
,
nullable
:
false
)
},
constraints
:
table
=>
...
...
Clinics.Backend/Persistence/Migrations/ClinicsDbContextModelSnapshot.cs
View file @
45a07430
...
...
@@ -25,11 +25,8 @@ namespace Persistence.Migrations
modelBuilder
.
Entity
(
"Domain.Entities.Identity.UserRoles.Role"
,
b
=>
{
b
.
Property
<
int
>(
"Id"
)
.
ValueGeneratedOnAdd
()
.
HasColumnType
(
"int"
);
SqlServerPropertyBuilderExtensions
.
UseIdentityColumn
(
b
.
Property
<
int
>(
"Id"
));
b
.
Property
<
string
>(
"Name"
)
.
IsRequired
()
.
HasMaxLength
(
50
)
...
...
Clinics.Backend/Persistence/SeedDatabase/UserRoles.cs
0 → 100644
View file @
45a07430
using
Domain.Entities.Identity.UserRoles
;
using
Microsoft.EntityFrameworkCore
;
using
Persistence.Context
;
namespace
Persistence.SeedDatabase
;
public
class
UserRoles
:
ISeed
<
Role
>
{
#
region
CTOR
DI
private
readonly
ClinicsDbContext
_context
;
public
UserRoles
(
ClinicsDbContext
context
)
{
_context
=
context
;
}
#
endregion
public
async
Task
Seed
()
{
DbSet
<
Role
>
roles
=
_context
.
Set
<
Role
>();
var
current
=
await
roles
.
ToListAsync
();
// TODO: perform deep check on all seed operations
if
(
current
.
Count
!=
Roles
.
Count
)
{
roles
.
RemoveRange
(
current
);
roles
.
Add
(
Roles
.
Admin
);
roles
.
Add
(
Roles
.
Doctor
);
roles
.
Add
(
Roles
.
Receptionist
);
await
_context
.
SaveChangesAsync
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment