-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAppIdentityDbContextSeed.cs
More file actions
120 lines (102 loc) · 3.96 KB
/
AppIdentityDbContextSeed.cs
File metadata and controls
120 lines (102 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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();
var scopedProvider = scope.ServiceProvider;
try
{
var userManager = scopedProvider.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = scopedProvider.GetRequiredService<RoleManager<IdentityRole>>();
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);
if (foundUser == null)
{
throw new Exception($"User not created.");
}
// ASP.NET Core Identity: Assigning role to user.
var rolledUser = await userManager.AddToRoleAsync(applicationUser, user.Role);
if (!rolledUser.Succeeded)
{
throw new Exception($"{user.UserName} is not enrolled.");
}
}
}
catch (Exception ex)
{
app.Logger.LogError($"An error occurred seeding the DB: {exception.Message}");
}
}
#endregion
#region Private Methods
private static IList<UserToSeed> GetListOfUsersToSeed()
{
return new List<UserToSeed>()
{
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<RolesToSeed> GetListOfRolesToSeed()
{
return new List<RolesToSeed>()
{
new RolesToSeed() { RoleName = RoleDefinition.ADMINISTRATOR_ROLE },
new RolesToSeed() { RoleName = RoleDefinition.USER_ROLE }
};
}
#endregion
}