diff --git a/APIwebASPNETCore6JWT/APIwebASPNETCore6JWT.csproj b/APIwebASPNETCore6JWT/APIwebASPNETCore6JWT.csproj
index 6f5f8e5..cb99e01 100644
--- a/APIwebASPNETCore6JWT/APIwebASPNETCore6JWT.csproj
+++ b/APIwebASPNETCore6JWT/APIwebASPNETCore6JWT.csproj
@@ -24,7 +24,6 @@
-
diff --git a/APIwebASPNETCore6JWT/Identity/AppIdentityDbContextSeed.cs b/APIwebASPNETCore6JWT/Identity/AppIdentityDbContextSeed.cs
index e622248..79b31fe 100644
--- a/APIwebASPNETCore6JWT/Identity/AppIdentityDbContextSeed.cs
+++ b/APIwebASPNETCore6JWT/Identity/AppIdentityDbContextSeed.cs
@@ -1,9 +1,12 @@
using Microsoft.AspNetCore.Identity;
+using System;
namespace APIwebASPNETCore6JWT.Identity;
public static class AppIdentityDbContextSeed
{
+ #region Public Methods
+
public static async Task SeedAsync(WebApplication app)
{
using var scope = app.Services.CreateScope();
@@ -13,23 +16,105 @@ public static async Task SeedAsync(WebApplication app)
var userManager = scopedProvider.GetRequiredService>();
var roleManager = scopedProvider.GetRequiredService>();
- await roleManager.CreateAsync(new IdentityRole("Administrator"));
+ var roles = GetListOfRolesToSeed();
+ var users = GetListOfUsersToSeed();
+
+ // 1. ASP.NET Core Identity: Add roles.
+ foreach (var role in roles)
+ {
+ // Asp Net Identity: Create roles to assign user after.
+ var createdRole = await roleManager.CreateAsync(new IdentityRole(role.RoleName));
+
+ if (!createdRole.Succeeded)
+ {
+ throw new Exception($"role {role.RoleName} not created.");
+ }
+ }
+
+ // 2. ASP.NET Core Identity: Add users and assigning roles.
+ foreach (var user in users)
+ {
+ var applicationUser = new ApplicationUser()
+ {
+ UserName = user.UserName,
+ Email = user.Email,
+ PhoneNumber = user.PhoneNumber,
+ };
+
+ // ASP.NET Core Identity: Create user.
+ var createUserTaskResult = await userManager.CreateAsync(applicationUser, user.Password);
+
+ if (!createUserTaskResult.Succeeded)
+ {
+ throw new Exception($"User {applicationUser.UserName} not created.");
+ }
+
+ // ASP.NET Core Identity: Finding user to assign role.
+ var foundUser = await userManager.FindByNameAsync(user.UserName);
- var defaultUser = new ApplicationUser { UserName = "user@test.com", Email = "user@test.com" };
- await userManager.CreateAsync(defaultUser, "P@ss.W0rd");
+ if (foundUser == null)
+ {
+ throw new Exception($"User not created.");
+ }
- string adminUserName = "admin@test.com";
- var adminUser = new ApplicationUser { UserName = adminUserName, Email = adminUserName };
- await userManager.CreateAsync(adminUser, "P@ss.W0rd");
+ // ASP.NET Core Identity: Assigning role to user.
+ var rolledUser = await userManager.AddToRoleAsync(applicationUser, user.Role);
- adminUser = await userManager.FindByNameAsync(adminUserName);
- await userManager.AddToRoleAsync(adminUser, "Administrator");
+ if (!rolledUser.Succeeded)
+ {
+ throw new Exception($"{user.UserName} is not enrolled.");
+ }
+ }
}
catch (Exception ex)
{
- app.Logger.LogError(ex, "An error occurred seeding the DB.");
+ app.Logger.LogError($"An error occurred seeding the DB: {exception.Message}");
}
}
-}
+ #endregion
+
+ #region Private Methods
+
+ private static IList GetListOfUsersToSeed()
+ {
+ return new List()
+ {
+ new UserToSeed()
+ {
+ UserName = "javi.karra",
+ Password = "P@ss.W0rd@javi",
+ Email = "javi.karra@mycompany.com",
+ PhoneNumber = "1234567890",
+ Role = RoleDefinition.USER_ROLE,
+ },
+ new UserToSeed()
+ {
+ UserName = "lucas.perez",
+ Password = "P@ss.W0rd@lucas",
+ Email = "lucas.perez@mycompany.com",
+ PhoneNumber = "6234567890",
+ Role = RoleDefinition.USER_ROLE,
+ },
+ new UserToSeed()
+ {
+ UserName = "admin",
+ Password = "P@ss.W0rd@admin",
+ Email = "admin@test.com",
+ PhoneNumber = "623455699",
+ Role = RoleDefinition.ADMINISTRATOR_ROLE,
+ }
+ };
+ }
+
+ private static IList GetListOfRolesToSeed()
+ {
+ return new List()
+ {
+ new RolesToSeed() { RoleName = RoleDefinition.ADMINISTRATOR_ROLE },
+ new RolesToSeed() { RoleName = RoleDefinition.USER_ROLE }
+ };
+ }
+ #endregion
+}
\ No newline at end of file
diff --git a/APIwebASPNETCore6JWT/Identity/RoleDefinition.cs b/APIwebASPNETCore6JWT/Identity/RoleDefinition.cs
new file mode 100644
index 0000000..62fa2cd
--- /dev/null
+++ b/APIwebASPNETCore6JWT/Identity/RoleDefinition.cs
@@ -0,0 +1,13 @@
+namespace APIwebASPNETCore6JWT.Identity
+{
+ public static class RoleDefinition
+ {
+ #region Fiedls
+
+ public const string ADMINISTRATOR_ROLE = "Administrator";
+
+ public const string USER_ROLE = "User";
+
+ #endregion
+ }
+}
diff --git a/APIwebASPNETCore6JWT/Identity/RolesToSeed.cs b/APIwebASPNETCore6JWT/Identity/RolesToSeed.cs
new file mode 100644
index 0000000..ed85357
--- /dev/null
+++ b/APIwebASPNETCore6JWT/Identity/RolesToSeed.cs
@@ -0,0 +1,7 @@
+namespace APIwebASPNETCore6JWT.Identity
+{
+ public class RolesToSeed
+ {
+ public string RoleName { get; set; } = default!;
+ }
+}
diff --git a/APIwebASPNETCore6JWT/Identity/UserToSeed.cs b/APIwebASPNETCore6JWT/Identity/UserToSeed.cs
new file mode 100644
index 0000000..dbf73f5
--- /dev/null
+++ b/APIwebASPNETCore6JWT/Identity/UserToSeed.cs
@@ -0,0 +1,13 @@
+namespace APIwebASPNETCore6JWT.Identity
+{
+ public class UserToSeed
+ {
+ public string Email { get; set; } = default!;
+ public string FirstName { get; set; } = default!;
+ public string LastName { get; set; } = default!;
+ public string Password { get; set; } = default!;
+ public string PhoneNumber { get; set; } = default!;
+ public string Role { get; set; } = default!;
+ public string UserName { get; set; } = default!;
+ }
+}