using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BlazorApp.Areas.Identity;
using BlazorApp.Data;
using Microsoft.AspNetCore.Identity.UI.Services;
namespace BlazorApp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
//services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
// .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<IdentityUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
services.AddSingleton<WeatherForecastService>();
services.AddSingleton<IEmailSender, EmailSender>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
// PM> Update-Database required
CreateBuiltInUsersAndRoles(serviceProvider).Wait();
}
private async Task CreateBuiltInUsersAndRoles(IServiceProvider serviceProvider)
{
//[0] DbContext 개체 생성
var dbContext = serviceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.EnsureCreated(); // 데이터베이스가 생성되어 있는지 확인
// 기본 내장 사용자 및 역할이 하나도 없으면(즉, 처음 데이터베이스 생성이라면)
if (!dbContext.Users.Any() && !dbContext.Roles.Any())
{
string domainName = "a.com";
//[1] Groups(Roles)
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
//[1][1] ('Administrators', '관리자 그룹', 'Group', '응용 프로그램을 총 관리하는 관리 그룹 계정')
//[1][2] ('Everyone', '전체 사용자 그룹', 'Group', '응용 프로그램을 사용하는 모든 사용자 그룹 계정')
//[1][3] ('Users', '일반 사용자 그룹', 'Group', '일반 사용자 그룹 계정')
//[1][4] ('Guests', '관리자 그룹', 'Group', '게스트 사용자 그룹 계정')
string[] roleNames = { "Administrators", "Everyone", "Users", "Guests" };
foreach (var roleName in roleNames)
{
var roleExist = await roleManager.RoleExistsAsync(roleName);
if (!roleExist)
{
await roleManager.CreateAsync(new IdentityRole(roleName)); // 빌트인 그룹 생성
}
}
//[2] Users
var userManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
//[2][1] Administrator
// ('Administrator', '관리자', 'User', '응용 프로그램을 총 관리하는 사용자 계정')
IdentityUser administrator = await userManager.FindByEmailAsync($"administrator@{domainName}");
if (administrator == null)
{
administrator = new IdentityUser()
{
UserName = "Administrator",
Email = $"administrator@{domainName}",
};
await userManager.CreateAsync(administrator, "Pa$$w0rd");
}
//[2][2] Guest
// ('Guest', '게스트 사용자', 'User', '게스트 사용자 계정')
IdentityUser guest = await userManager.FindByEmailAsync($"guest@{domainName}");
if (guest == null)
{
guest = new IdentityUser()
{
UserName = "Guest",
Email = $"guest@{domainName}",
};
await userManager.CreateAsync(guest, "Pa$$w0rd");
}
//[2][3] Anonymous
// ('Anonymous', '익명 사용자', 'User', '익명 사용자 계정')
IdentityUser anonymous = await userManager.FindByEmailAsync($"anonymous@{domainName}");
if (anonymous == null)
{
anonymous = new IdentityUser()
{
UserName = "Anonymous",
Email = $"anonymous@{domainName}",
};
await userManager.CreateAsync(anonymous, "Pa$$w0rd");
}
//[3] UsersInRoles: AspNetUserRoles Table
await userManager.AddToRoleAsync(administrator, "Administrators");
await userManager.AddToRoleAsync(administrator, "Users");
await userManager.AddToRoleAsync(guest, "Guests");
await userManager.AddToRoleAsync(anonymous, "Guests");
}
}
}
}