-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProgram.cs
More file actions
145 lines (113 loc) · 3.88 KB
/
Program.cs
File metadata and controls
145 lines (113 loc) · 3.88 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using PrjFunNowWeb.Models;
using System.Configuration;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Authentication.Cookies;
using PrjFunNowWeb.Models.DTO;
using PrjFunNowWeb.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(GoogleDefaults.AuthenticationScheme, options =>
{
options.ClientId = builder.Configuration.GetSection("GoogleKeys:ClientID").Value;
options.ClientSecret = builder.Configuration.GetSection("GoogleKeys:ClientSecret").Value;
});
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddHttpClient(); // 添加HttpClient服務
builder.Services.AddDbContext<FunNowContext>(
options => options.UseSqlServer(
builder.Configuration.GetConnectionString("FunNowConnection")
));
// 添加 HttpClient 服务
builder.Services.AddHttpClient();
// 添加 Session 服務
builder.Services.AddSession(options =>
{
// 設置 Session 的 cookie 名稱
options.Cookie.Name = ".YourApp.Session";
// 設置 Session 的過期時間
options.IdleTimeout = TimeSpan.FromMinutes(30);
// 設置 cookie 是不是只在 HTTPS 中有效
options.Cookie.HttpOnly = true;
// 設置 cookie 的安全等級
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.IsEssential = true;
});
builder.Services.Configure<GoogleCaptchaConfig>(builder.Configuration.GetSection("GoogleReCaptcha"));
builder.Services.AddCors(options =>
{
// 定義允許特定來源的策略
options.AddPolicy("AllowSpecificOrigin",
builder => builder.WithOrigins("https://localhost:7103") // 替換為你的前端域名
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
// Add SignalR client services if needed for SignalR client side (optional)
builder.Services.AddSignalR();
//Angular用
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAngularApp",
builder =>
{
builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddControllersWithViews();
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
});
// 配置 CORS 策略
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
// 配置HTTP請求管道
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
//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.UseSession(); //註冊Session 服務
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
//app.UseMiddleware<AuthenticationMiddleware>();
// 使用 CORS
app.UseCors("AllowAllOrigins");
app.UseAuthorization();
app.UseCors("AllowAngularApp");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
//pattern: "{controller=HotelCreate}/{action=HotelInfo}/{id?}");
//// 配置路由以支持 Angular 路由
//app.MapFallbackToFile("/dist/fun-now-angular1/index.html");
app.Run();