diff --git a/Hcs.WebApp/Components/Pages/Account/Login.razor b/Hcs.WebApp/Components/Pages/Account/Login.razor
index dd5f11d..898038f 100644
--- a/Hcs.WebApp/Components/Pages/Account/Login.razor
+++ b/Hcs.WebApp/Components/Pages/Account/Login.razor
@@ -14,7 +14,7 @@
-
+
diff --git a/Hcs.WebApp/Components/Pages/Account/Register.razor b/Hcs.WebApp/Components/Pages/Account/Register.razor
index f4eaee1..0f20561 100644
--- a/Hcs.WebApp/Components/Pages/Account/Register.razor
+++ b/Hcs.WebApp/Components/Pages/Account/Register.razor
@@ -14,7 +14,7 @@
-
+
diff --git a/Hcs.WebApp/Controllers/IdentityController.cs b/Hcs.WebApp/Controllers/IdentityController.cs
index 5711677..6593ff9 100644
--- a/Hcs.WebApp/Controllers/IdentityController.cs
+++ b/Hcs.WebApp/Controllers/IdentityController.cs
@@ -39,7 +39,7 @@ namespace Hcs.WebApp.Controllers
if (string.IsNullOrEmpty(returnUrl))
{
- Redirect("/");
+ return Redirect("/");
}
return Redirect(returnUrl);
@@ -63,7 +63,7 @@ namespace Hcs.WebApp.Controllers
if (string.IsNullOrEmpty(returnUrl))
{
- Redirect("/");
+ return Redirect("/");
}
return Redirect(returnUrl);
diff --git a/Hcs.WebApp/Data/AppIdentityDbContext.cs b/Hcs.WebApp/Data/AppIdentityDbContext.cs
index 391264d..40a9795 100644
--- a/Hcs.WebApp/Data/AppIdentityDbContext.cs
+++ b/Hcs.WebApp/Data/AppIdentityDbContext.cs
@@ -1,7 +1,143 @@
-using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
+using Microsoft.AspNetCore.Identity;
+using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
namespace Hcs.WebApp.Data
{
- public class AppIdentityDbContext(DbContextOptions options) : IdentityDbContext(options) { }
+ public class AppIdentityDbContext(DbContextOptions options) : IdentityDbContext(options)
+ {
+ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
+ {
+ optionsBuilder
+ .UseSeeding((context, _) =>
+ {
+ var adminRole = context.Set().FirstOrDefault(x => x.Name == AppRole.ADMINISTRATOR_TYPE);
+ if (adminRole == null)
+ {
+ adminRole = new AppRole()
+ {
+ Name = AppRole.ADMINISTRATOR_TYPE,
+ NormalizedName = AppRole.ADMINISTRATOR_TYPE.Normalize()
+ };
+ context.Set().Add(adminRole);
+ }
+
+ var operatorRole = context.Set().FirstOrDefault(x => x.Name == AppRole.OPERATOR_TYPE);
+ if (operatorRole == null)
+ {
+ context.Set().Add(new AppRole()
+ {
+ Name = AppRole.OPERATOR_TYPE,
+ NormalizedName = AppRole.OPERATOR_TYPE.Normalize()
+ });
+ }
+
+ var observerRole = context.Set().FirstOrDefault(x => x.Name == AppRole.OBSERVER_TYPE);
+ if (observerRole == null)
+ {
+ context.Set().Add(new AppRole()
+ {
+ Name = AppRole.OBSERVER_TYPE,
+ NormalizedName = AppRole.OBSERVER_TYPE.Normalize()
+ });
+ }
+
+ var adminUser = context.Set().FirstOrDefault(x => x.UserName == AppUser.ADMINISTRATOR_USER_NAME);
+ var hasAdmin = adminUser != null;
+ if (adminUser == null)
+ {
+ adminUser = new AppUser()
+ {
+ UserName = AppUser.ADMINISTRATOR_USER_NAME,
+ NormalizedUserName = AppUser.ADMINISTRATOR_USER_NAME.Normalize(),
+ LockoutEnabled = true
+ };
+
+ var passwordHasher = context.GetService>();
+ adminUser.PasswordHash = passwordHasher.HashPassword(adminUser, AppUser.ADMINISTRATOR_PASSWORD);
+
+ var entry = context.Set().Add(adminUser);
+ }
+
+ context.SaveChanges();
+
+ if (!hasAdmin)
+ {
+ context.Set>().Add(new IdentityUserRole()
+ {
+ UserId = adminUser.Id,
+ RoleId = adminRole.Id
+ });
+ }
+
+ context.SaveChanges();
+ })
+ .UseAsyncSeeding(async (context, _, cancellationToken) =>
+ {
+ var adminRole = await context.Set().FirstOrDefaultAsync(x => x.Name == AppRole.ADMINISTRATOR_TYPE, cancellationToken);
+ if (adminRole == null)
+ {
+ adminRole = new AppRole()
+ {
+ Name = AppRole.ADMINISTRATOR_TYPE,
+ NormalizedName = AppRole.ADMINISTRATOR_TYPE.Normalize()
+ };
+ context.Set().Add(adminRole);
+ }
+
+ var operatorRole = await context.Set().FirstOrDefaultAsync(x => x.Name == AppRole.OPERATOR_TYPE, cancellationToken);
+ if (operatorRole == null)
+ {
+ context.Set().Add(new AppRole()
+ {
+ Name = AppRole.OPERATOR_TYPE,
+ NormalizedName = AppRole.OPERATOR_TYPE.Normalize()
+ });
+ }
+
+ var observerRole = await context.Set().FirstOrDefaultAsync(x => x.Name == AppRole.OBSERVER_TYPE, cancellationToken);
+ if (observerRole == null)
+ {
+ context.Set().Add(new AppRole()
+ {
+ Name = AppRole.OBSERVER_TYPE,
+ NormalizedName = AppRole.OBSERVER_TYPE.Normalize()
+ });
+ }
+
+ var adminUser = await context.Set().FirstOrDefaultAsync(x => x.UserName == AppUser.ADMINISTRATOR_USER_NAME, cancellationToken);
+ var hasAdmin = adminUser != null;
+ if (!hasAdmin)
+ {
+ adminUser = new AppUser()
+ {
+ UserName = AppUser.ADMINISTRATOR_USER_NAME,
+ NormalizedUserName = AppUser.ADMINISTRATOR_USER_NAME.Normalize(),
+ LockoutEnabled = true
+ };
+
+ var passwordHasher = context.GetService>();
+ adminUser.PasswordHash = passwordHasher.HashPassword(adminUser, AppUser.ADMINISTRATOR_PASSWORD);
+
+ var entry = context.Set().Add(adminUser);
+ }
+
+ await context.SaveChangesAsync();
+
+ if (!hasAdmin)
+ {
+ context.Set>().Add(new IdentityUserRole()
+ {
+ UserId = adminUser.Id,
+ RoleId = adminRole.Id
+ });
+ }
+
+ await context.SaveChangesAsync();
+ });
+
+ base.OnConfiguring(optionsBuilder);
+ }
+ }
}
diff --git a/Hcs.WebApp/Data/AppRole.cs b/Hcs.WebApp/Data/AppRole.cs
index dab2600..e69a37d 100644
--- a/Hcs.WebApp/Data/AppRole.cs
+++ b/Hcs.WebApp/Data/AppRole.cs
@@ -2,5 +2,10 @@
namespace Hcs.WebApp.Data
{
- public class AppRole : IdentityRole { }
+ public class AppRole : IdentityRole
+ {
+ public const string ADMINISTRATOR_TYPE = "Administrator";
+ public const string OPERATOR_TYPE = "Operator";
+ public const string OBSERVER_TYPE = "Observer";
+ }
}
diff --git a/Hcs.WebApp/Data/AppUser.cs b/Hcs.WebApp/Data/AppUser.cs
index 1abbf81..478e545 100644
--- a/Hcs.WebApp/Data/AppUser.cs
+++ b/Hcs.WebApp/Data/AppUser.cs
@@ -2,5 +2,9 @@
namespace Hcs.WebApp.Data
{
- public class AppUser : IdentityUser { }
+ public class AppUser : IdentityUser
+ {
+ public const string ADMINISTRATOR_USER_NAME = "admin";
+ public const string ADMINISTRATOR_PASSWORD = "qaz159357";
+ }
}
diff --git a/Hcs.WebApp/Data/Migrations/AppIdentityDbContextModelSnapshot.cs b/Hcs.WebApp/Data/Migrations/AppIdentityDbContextModelSnapshot.cs
index a63d47d..cc2a88b 100644
--- a/Hcs.WebApp/Data/Migrations/AppIdentityDbContextModelSnapshot.cs
+++ b/Hcs.WebApp/Data/Migrations/AppIdentityDbContextModelSnapshot.cs
@@ -1,10 +1,11 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
+#nullable disable
namespace Hcs.WebApp.Data.Migrations
{
[DbContext(typeof(AppIdentityDbContext))]
- public class AppIdentityDbContextModelSnapshot : ModelSnapshot
+ public partial class AppIdentityDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
@@ -80,7 +81,7 @@ namespace Hcs.WebApp.Data.Migrations
b.ToTable("AspNetUsers", (string)null);
});
- modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
+ modelBuilder.Entity("Hcs.WebApp.Data.AppRole", b =>
{
b.Property("Id")
.HasColumnType("nvarchar(450)");
diff --git a/Hcs.WebApp/Hcs.WebApp.csproj b/Hcs.WebApp/Hcs.WebApp.csproj
index 3ae1000..8abc3fc 100644
--- a/Hcs.WebApp/Hcs.WebApp.csproj
+++ b/Hcs.WebApp/Hcs.WebApp.csproj
@@ -15,8 +15,9 @@
-
-
+
+
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive