-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoleInitializer.cs
36 lines (34 loc) · 1.18 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
36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using DBIT.Models;
namespace DBIT
{
public class RoleInitializer
{
public static async Task InitializeAsync(UserManager<User> userManager, RoleManager<IdentityRole> roleManager)
{
string adminEmail = "[email protected]";
string password = "Qwerty_1";
if (await roleManager.FindByNameAsync("admin") == null)
{
await roleManager.CreateAsync(new IdentityRole("admin"));
}
if (await roleManager.FindByNameAsync("user") == null)
{
await roleManager.CreateAsync(new IdentityRole("user"));
}
if (await userManager.FindByNameAsync(adminEmail) == null)
{
User admin = new User { Email = adminEmail, UserName = adminEmail };
IdentityResult result = await userManager.CreateAsync(admin, password);
if (result.Succeeded)
{
await userManager.AddToRoleAsync(admin, "admin");
}
}
}
}
}