-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
86 lines (69 loc) · 2.39 KB
/
Program.cs
File metadata and controls
86 lines (69 loc) · 2.39 KB
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
using CookingNotebookWebApp.Data;
using CookingNotebookWebApp.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.Cookies;
var builder = WebApplication.CreateBuilder(args);
// Đăng ký DbContext
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// Đăng ký Services
builder.Services.AddScoped<MealPlanningService>();
builder.Services.AddScoped(provider =>
new AppAuthenticationService(
provider.GetRequiredService<AppDbContext>(),
builder.Configuration["Google:ClientId"] ?? ""
)
);
// Thêm xác thực cookie
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.AccessDeniedPath = "/Account/AccessDenied";
options.Cookie.Name = "CookingNotebook.Auth";
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromDays(7);
options.SlidingExpiration = true;
});
// Thêm MVC (Controllers + Views)
builder.Services.AddControllersWithViews();
// Thêm chính sách CORS
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
builder.Services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
policy =>
{
policy.WithOrigins("http://localhost:5000", "http://localhost", "https://localhost:7089", "http://localhost:5083")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
// Khởi tạo database
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
dbContext.Database.Migrate();
}
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
// Kích hoạt CORS
app.UseCors(MyAllowSpecificOrigins);
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
// Map API controllers
app.MapControllers();
// Cấu hình route mặc định vào Login
app.MapControllerRoute(
name: "default",
pattern: "{controller=Homepage}/{action=Homepage}/{id?}"
);
app.Run();