-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (91 loc) · 3.15 KB
/
Program.cs
File metadata and controls
110 lines (91 loc) · 3.15 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.OpenApi.Models;
using MyApi.Middlewares;
using MyApi.Services;
using Serilog;
using System.Diagnostics;
//התקנת תעודת אבטחה dotnet dev-certs https --trust
//? לעשות שלמנהל יהיה אופציה להוסיף מתנה למשתמש כלשהו (ע"י בחירת השם/תז שלו)
var builder = WebApplication.CreateBuilder(args);
// הוספת שירותי ה-Controllers
builder.Services.AddAuthentication(optiens =>
{
optiens.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(cfg =>
{
cfg.RequireHttpsMetadata = false;
cfg.TokenValidationParameters = TokenService.GetTokenValidationParameters();
});
builder.Services.AddAuthorization(cfg =>
{
cfg.AddPolicy("Admin",
policy => policy.RequireClaim("type","Admin"));
cfg.AddPolicy("User",
policy => policy.RequireClaim("type","User","Admin"));
});
builder.Services.AddControllers();
builder.Services.AddCurrentUser();
builder.Services.AddGiftJson();
builder.Services.AddUserJson();
// הוספת Swagger
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please enter JWT with Bearer into field",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{ new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer"}
},
new string[] {}
}
});
});
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
builder.Host.UseSerilog(); // Use Serilog for logging
var app = builder.Build();
// קביעת המידלוואר לשרת את Swagger כנקודת JSON
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
// app.UseSwaggerUI(c =>
// {
// c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
// c.RoutePrefix = string.Empty; // קבעי את Swagger UI בשורש האפליקציה
// });
app.UseSwaggerUI();
}
// app.UseMyLog();
app.Use(async (context, next) =>
{
var sw = new Stopwatch();
sw.Start();
await next(context);
sw.Stop();
Log.Information("{Path} {Method} took {ElapsedMilliseconds}ms", context.Request.Path, context.Request.Method, sw.ElapsedMilliseconds);
});
app.UseMyErrorMiddleware();
//app.UseHttpsRedirection();
/*js*/
app.UseDefaultFiles();
app.UseStaticFiles();
/*js (remove "launchUrl" from Properties\launchSettings.json*/
app.UseRouting();
app.UseTokenMiddleware();
app.UseAuthentication();
//app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();