summaryrefslogtreecommitdiff
path: root/Models/IdentitySeedData.cs
blob: dcaa5c3de3a16bcbc4fa0644872f1ea1de0f9ff0 (plain)
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
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);			
		}
	}	
}