using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddDefaultIdentity<IdentityUser>(options =>
{
options.SignIn.RequireConfirmedAccount = false;
options.Password.RequiredLength = 8;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var roles = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var users = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
const string adminRole = "Administrator";
if (!await roles.RoleExistsAsync(adminRole))
await roles.CreateAsync(new IdentityRole(adminRole));
const string adminEmail = "admin@local";
var admin = await users.FindByEmailAsync(adminEmail);
if (admin is null)
{
admin = new IdentityUser { UserName = adminEmail, Email = adminEmail };
await users.CreateAsync(admin, "ChangeMe_123!");
await users.AddToRoleAsync(admin, adminRole);
}
}
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddDefaultIdentity<IdentityUser>(options =>
{
options.SignIn.RequireConfirmedAccount = false;
options.Password.RequiredLength = 8;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var roles = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var users = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
const string adminRole = "Administrator";
if (!await roles.RoleExistsAsync(adminRole))
await roles.CreateAsync(new IdentityRole(adminRole));
const string adminEmail = "admin@local";
var admin = await users.FindByEmailAsync(adminEmail);
if (admin is null)
{
admin = new IdentityUser { UserName = adminEmail, Email = adminEmail };
await users.CreateAsync(admin, "ChangeMe_123!");
await users.AddToRoleAsync(admin, adminRole);
}
}
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();