-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeedData.cs
More file actions
37 lines (34 loc) · 1.24 KB
/
SeedData.cs
File metadata and controls
37 lines (34 loc) · 1.24 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
using Microsoft.AspNetCore.Identity;
using OnlineShoppingSite.Models;
using System.Threading.Tasks;
namespace OnlineShoppingSite
{
public static class SeedData
{
public static async Task InitializeAsync(
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager)
{
string adminRole = "Admin";
string adminEmail = "admin@example.com";
string adminPassword = "Admin@123";
// Create Admin role if it doesn't exist
if (!await roleManager.RoleExistsAsync(adminRole))
{
var role = new IdentityRole(adminRole);
await roleManager.CreateAsync(role);
}
// Create Admin user if it doesn't exist
var adminUser = await userManager.FindByEmailAsync(adminEmail);
if (adminUser == null)
{
adminUser = new ApplicationUser { UserName = adminEmail, Email = adminEmail };
var result = await userManager.CreateAsync(adminUser, adminPassword);
if (result.Succeeded)
{
await userManager.AddToRoleAsync(adminUser, adminRole);
}
}
}
}
}