-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
87 lines (71 loc) · 2.9 KB
/
Program.cs
File metadata and controls
87 lines (71 loc) · 2.9 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.EntityFrameworkCore;
using Winter_Project.Models;
using Winter_Project.Services;
using System.Text;
using System.Text.Json;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddScoped<UserService>();
builder.Services.AddScoped<NotificationService>();
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<WebSocketHandler>();
var jwtSettings = builder.Configuration.GetSection("Jwt");
var key = Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!);
// ไว้ set ให้ วันที่เป็นภาษาอังกฤษ
var cultureInfo = new System.Globalization.CultureInfo("en-US");
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
// Configure JWT Bearer Authentication
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true, // Set to true if you want to validate the issuer
ValidateAudience = true, // Set to true if you want to validate the audience
ValidateLifetime = true,
ValidIssuer = jwtSettings["Issuer"], // กำหนดค่า Issuer จากการตั้งค่า
ValidAudience = jwtSettings["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(key),
ClockSkew = TimeSpan.Zero // Optional: prevents delay between token generation and validation
};
});
// Add DbContext for WinterContext
builder.Services.AddDbContext<WinterContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"))
);
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowFrontend",
policy =>
{
policy.WithOrigins("https://yourfrontend.com") // เปลี่ยนเป็นโดเมนของคุณ
.AllowCredentials()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseStaticFiles(); // Serves static files from wwwroot
app.UseHttpsRedirection();
app.UseRouting();
// Enable authentication and authorization
app.UseAuthentication();
app.UseAuthorization();
//Enable WebSockets
app.UseWebSockets();
// Map controller routes for MVC
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();