Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
114 lines (99 loc) · 4.66 KB
/
Copy pathProgram.cs
File metadata and controls
114 lines (99 loc) · 4.66 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using System.Text;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Configuration;
using FA23_Convocation2023_API.Hubs;
using Microsoft.AspNetCore.Localization;
using System.Globalization;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc;
using FA23_Convocation2023_API.Models;
using FA23_Convocation2023_API.Services;
namespace FA23_Convocation2023_API
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
//builder.Services.AddControllers().AddJsonOptions(options =>
//{
// options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
//});
//add signalR
builder.Services.AddSignalR();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
// Add DbContext
builder.Services.AddDbContext<Convo24Context>();
// Add CORS
builder.Services.AddCors(options => {
options.AddPolicy("CORSPolicy", builder =>
builder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().SetIsOriginAllowed((hosts) => true));
});
// Chỗ này chỉ để config lúc login xong mình sẽ
// lấy token, ở trên có nút Authorize, ấn vô
// sẽ mở modal để mình bỏ token để phân quyền á, kiểu v
// Nói chung chỉ để config cái hộp đấy thoi à
builder.Services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Description = "Standard Authorization header using the Bearer scheme (\"bearer {token}\")",
In = ParameterLocation.Header,
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
options.OperationFilter<SecurityRequirementsOperationFilter>();
});
// Chỗ này để add authen thôi, với config tạo cái token á
builder.Services.AddAuthentication(options =>
{ // khúc này config cái authen thôi bỏ qua cũng đc
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
// khúc này config cái access_token nè
}).AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters //config thông qua các params á
{
// nói chung khúc này để validate cái token thôi
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(builder.Configuration.GetSection("JWT:Key").Value)),
ValidateIssuer = false,
ValidateAudience = false
};
});
// Add chức năng phân quyền nè, 1 dòng :)))
builder.Services.AddAuthorization();
builder.Services.AddScoped<BachelorService>()
.AddScoped<CheckInService>()
.AddScoped<HallService>()
.AddScoped<SessionService>();
// Nhớ là nếu cái gì liên quan tới config builder như builder.Services. gì á,
// thì nhớ là phải bỏ trên dòng này nha, tại từ dòng này trở xuống là
// config kh được, do nó build r á
var app = builder.Build();
// Khúc này trở xuống đơn giản là project có gì lấy ra xài thoi
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();
// Add Cors 1 dòng :))))
app.UseCors("CORSPolicy");
app.UseRouting();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapHub<MessageHub>("chat-hub");
app.MapControllers();
app.Run();
}
}
}