-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoleInitializer.cs
35 lines (33 loc) · 1.32 KB
/
RoleInitializer.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
using Microsoft.AspNetCore.Identity;
namespace MIL_LIT
{
public class RoleInitializer
{
public static async Task InitializeAsync(UserManager<User> userManager, RoleManager<IdentityRole<int>> roleManager)
{
string adminEmail = "[email protected]";
string password = "Qwerty_1";
if (await roleManager.FindByNameAsync("admin") == null)
{
await roleManager.CreateAsync(new IdentityRole<int>("admin"));
}
if (await roleManager.FindByNameAsync("user") == null)
{
await roleManager.CreateAsync(new IdentityRole<int>("user"));
}
if (await roleManager.FindByNameAsync("moderator") == null)
{
await roleManager.CreateAsync(new IdentityRole<int>("moderator"));
}
if (await userManager.FindByNameAsync(adminEmail) == null)
{
User admin = new User { Email = adminEmail, UserName = adminEmail, PasswordHash = password, Login = adminEmail, IsAdmin = true};
IdentityResult result = await userManager.CreateAsync(admin, password);
if (result.Succeeded)
{
await userManager.AddToRoleAsync(admin, "admin");
}
}
}
}
}