auto.bus_api/Server/Data/SeedData.cs

34 lines
1.1 KiB
C#

using Microsoft.AspNetCore.Identity;
using Server.Models;
namespace Server.Data;
public class SeedData
{
public static async Task Initialize(IServiceProvider serviceProvider)
{
var userManager = (UserManager<User>)serviceProvider.GetService(typeof(UserManager<User>))!;
var roleManager = (RoleManager<IdentityRole>)serviceProvider.GetService(typeof(RoleManager<IdentityRole>))!;
//Seed Roles
foreach (var role in Enum.GetValues(typeof(Constants.Identity.Roles)))
{
await roleManager.CreateAsync(new IdentityRole(role.ToString()));
}
//Seed Default User
var defaultUser = new User
{
UserName = Constants.Identity.DefaultUsername,
Email = Constants.Identity.DefaultEmail,
EmailConfirmed = true
};
if (userManager.Users.All(u => u.Id != defaultUser.Id))
{
await userManager.CreateAsync(defaultUser, Constants.Identity.DefaultPassword);
await userManager.AddToRoleAsync(defaultUser, Constants.Identity.DefaultRole.ToString());
}
}
}