Files
Aberwyn/Aberwyn/Data/IdentityDataInitializer.cs
Elias Jansson 5169889753
Some checks failed
continuous-integration/drone/push Build is failing
EF conversion and local DB
2025-06-02 23:32:28 +02:00

45 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Aberwyn.Models;
namespace Aberwyn.Data
{
public static class IdentityDataInitializer
{
public static async Task SeedData(IServiceProvider serviceProvider)
{
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
string[] roles = { "Admin", "Chef", "Budget" };
foreach (var role in roles)
{
if (!await roleManager.RoleExistsAsync(role))
await roleManager.CreateAsync(new IdentityRole(role));
}
string adminUsername = "admin";
string adminEmail = "admin@localhost";
string password = "Admin123!";
if (await userManager.FindByEmailAsync(adminEmail) == null)
{
var user = new ApplicationUser
{
UserName = adminUsername,
Email = adminEmail,
EmailConfirmed = true
};
var result = await userManager.CreateAsync(user, password);
if (result.Succeeded)
{
await userManager.AddToRoleAsync(user, "Admin");
}
}
}
}
}