30 lines
946 B
C#
30 lines
946 B
C#
|
using Microsoft.AspNetCore.Identity;
|
||
|
|
||
|
namespace MyDarling.Models;
|
||
|
|
||
|
public class IdentitySeedData
|
||
|
{
|
||
|
public static void CreateAdminAccount(IServiceProvider serviceProvider, IConfiguration configuration)
|
||
|
{
|
||
|
CreateAdminAccountAsync(serviceProvider, configuration).Wait();
|
||
|
}
|
||
|
|
||
|
public static async Task CreateAdminAccountAsync(IServiceProvider serviceProvider, IConfiguration configuration)
|
||
|
{
|
||
|
serviceProvider = serviceProvider.CreateScope().ServiceProvider;
|
||
|
UserManager<IdentityUser> userManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
|
||
|
|
||
|
string username = configuration["ADMIN_USERNAME"] ?? "admin";
|
||
|
string password = configuration["ADMIN_PASSWORD"] ?? "Password123$";
|
||
|
|
||
|
if (await userManager.FindByNameAsync(username) == null)
|
||
|
{
|
||
|
IdentityUser adminUser = new IdentityUser
|
||
|
{
|
||
|
UserName = username
|
||
|
};
|
||
|
|
||
|
IdentityResult result = await userManager.CreateAsync(adminUser, password);
|
||
|
}
|
||
|
}
|
||
|
}
|