KoogleApp/src/Koogle.Infrastructure/Security/IdentityRoleSeeder.cs

30 lines
821 B
C#

using Koogle.Infrastructure.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Koogle.Infrastructure.Security;
public static class IdentityRoleSeeder
{
public static async Task SeedAsync(IServiceProvider services)
{
var roleManager = services.GetRequiredService<RoleManager<ApplicationRole>>();
string[] roles = { "SuperAdmin", "Admin", "Kassenwart", "Editor", "Viewer" };
foreach (var roleName in roles)
{
if (!await roleManager.RoleExistsAsync(roleName))
{
await roleManager.CreateAsync(new ApplicationRole { Name = roleName });
}
}
}
}