-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeedData.cs
47 lines (39 loc) · 1.41 KB
/
SeedData.cs
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
using aspnet_tut1.Data;
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
namespace aspnet_tut1
{
public static class SeedData
{
public static void Seed(UserManager<Employee> userManager,
RoleManager<IdentityRole> roleManager)
{
SeedRoles(roleManager, "administrator");
SeedUsers(userManager, "[email protected]", "administrator");
}
private static void SeedUsers(UserManager<Employee> userManager, string userName, string userRole)
{
if (userManager.FindByNameAsync(userName).Result == null)
{
var user = new Employee
{
UserName = userName,
Email = "[email protected]"
};
var userCreationResult = userManager.CreateAsync(user, "P@ssword1!!!").Result;
if (userCreationResult.Succeeded)
{
var result = userManager.AddToRoleAsync(user, userRole).Result;
}
}
}
private static void SeedRoles(RoleManager<IdentityRole> roleManager, string roleName)
{
var role = new IdentityRole { Name = roleName };
if (!roleManager.RoleExistsAsync(roleName).Result)
{
var result = roleManager.CreateAsync(role).Result;
}
}
}
}