This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
120 lines (94 loc) · 3.42 KB
/
Program.cs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Client;
using WebApp01.Models;
using WebApp01.Repository;
//using WebApp01.Sessions;
var builder = WebApplication.CreateBuilder(args);
// Connection DB
builder.Services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer(builder.Configuration["ConnectionStrings:ConnectedDb"]);
});
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(60); // Hen gio end Session
options.Cookie.IsEssential = true;
});
builder.Services.AddIdentity<AppUserModel,IdentityRole>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<DataContext>().AddDefaultTokenProviders();
builder.Services.Configure<IdentityOptions>(options =>
{
// Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 4;
//options.SignIn.RequireConfirmedAccount = false;
options.User.RequireUniqueEmail = true;
});
var app = builder.Build();
app.UseStatusCodePagesWithRedirects("/Home/Error?statuscode={0}");
app.UseSession();
app.UseStaticFiles();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
//app.UseHsts();
}
app.UseStaticFiles();
app.UseRouting();
//app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute( // Home Route ("/") cua trang admin
name: "Areas",
pattern: "{area:exists}/{controller=Product}/{action=Index}/{id?}");
app.MapControllerRoute( // Brand Route
name: "Brand",
pattern: "/Brand/{Slug?}",
defaults: new { controller = "Brand", action = "Index" });
app.MapControllerRoute( // Category Route
name: "Category",
pattern: "/Category/{Slug?}",
defaults: new {controller="Category",action="Index"});
app.MapControllerRoute( // Route default; cac route khac phai nam o tren default
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// seeding data
var context = app.Services.CreateScope().ServiceProvider.GetRequiredService<DataContext>();
SeedData.SeedingData(context);
using (var scope = app.Services.CreateScope())
{
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var roles = new[] { "Customer", "Admin" };
foreach (var role in roles)
{
if(!await roleManager.RoleExistsAsync(role))
{
await roleManager.CreateAsync(new IdentityRole(role));
}
}
}
//using (var scope = app.Services.CreateScope())
//{
// UserManager<IdentityUser> userManager = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
// string Email = "[email protected]";
// string Password = "Test@123";
// if(await userManager.FindByEmailAsync(Email) == null)
// {
// var user = new IdentityUser();
// user.UserName = Email;
// user.Email = Email;
// await userManager.CreateAsync(user, Password);
// await userManager.AddToRoleAsync(user, "Customer");
// }
//}
app.Run();