-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
132 lines (113 loc) · 4.77 KB
/
Program.cs
File metadata and controls
132 lines (113 loc) · 4.77 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System;
using System.Text;
using tsuKeysAPIProject.AdditionalServices.Exceptions;
using tsuKeysAPIProject.AdditionalServices.TokenHelpers;
using tsuKeysAPIProject.AdditionalServices.UserInfoHelper;
using tsuKeysAPIProject.DBContext;
using tsuKeysAPIProject.DBContext.Models;
using tsuKeysAPIProject.Services;
using tsuKeysAPIProject.Services.IServices.IKeyService;
using tsuKeysAPIProject.Services.IServices.IRequestService;
using tsuKeysAPIProject.Services.IServices.IRolesService;
using tsuKeysAPIProject.Services.IServices.IScheduleService;
using tsuKeysAPIProject.Services.IServices.IUserService;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext<AppDBContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddControllers();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IRoleService, RoleService>();
builder.Services.AddScoped<IKeyService, KeyService>();
builder.Services.AddScoped<IRequestService, RequestService>();
builder.Services.AddScoped<IScheduleService, ScheduleService>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddSingleton<TokenInteraction>();
builder.Services.AddScoped<UserInfoHelper>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "JWTToken",
ValidAudience = "Human",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("BACKENDDEVELOPINGFORHITSBACKENDDEVELOPINGFORHITSBACKENDDEVELOPINGFORHITSBACKENDDEVELOPINGFORHITSBACKENDDEVELOPINGFORHITSBACKENDDEVELOPINGFORHITS"))
};
});
builder.Services.AddAuthorization(services =>
{
services.AddPolicy("TokenNotInBlackList", policy => policy.Requirements.Add(new TokenBlackListRequirment()));
});
builder.Services.AddSingleton<IAuthorizationHandler, TokenInBlackListHandler>();
builder.Services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
{
Description = "",
Name = "Authorization",
In = Microsoft.OpenApi.Models.ParameterLocation.Header,
Scheme = "Bearer"
});
options.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header
},
new List<string>()
}
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
using var serviceScope = app.Services.CreateScope();
var dbContext = serviceScope.ServiceProvider.GetService<AppDBContext>();
//dbContext?.Database.Migrate();
dbContext.TimeSlots.RemoveRange(dbContext.TimeSlots);
dbContext.TimeSlots.AddRange(
new TimeSlot { SlotNumber = 1, StartTime = TimeOnly.Parse("08:45"), EndTime = TimeOnly.Parse("10:20") },
new TimeSlot { SlotNumber = 2, StartTime = TimeOnly.Parse("10:35"), EndTime = TimeOnly.Parse("12:10") },
new TimeSlot { SlotNumber = 3, StartTime = TimeOnly.Parse("12:25"), EndTime = TimeOnly.Parse("14:00") },
new TimeSlot { SlotNumber = 4, StartTime = TimeOnly.Parse("14:45"), EndTime = TimeOnly.Parse("16:20") },
new TimeSlot { SlotNumber = 5, StartTime = TimeOnly.Parse("16:35"), EndTime = TimeOnly.Parse("18:10") },
new TimeSlot { SlotNumber = 6, StartTime = TimeOnly.Parse("18:25"), EndTime = TimeOnly.Parse("20:00") }
);
dbContext.SaveChanges();
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.SetIsOriginAllowed(origin => true));
app.UseMiddleware<ExceptionMiddleware>();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();