-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
123 lines (105 loc) · 4.18 KB
/
Program.cs
File metadata and controls
123 lines (105 loc) · 4.18 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
using API.Data;
using API.Helpers;
using API.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using API.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Security.Claims;
using System.Text;
using API.Interfaces.SuperAdmin;
using API.Services.SuperAdmin;
using API.Services.Dashboard.Organization.Regions;
using API.Interfaces.Dashboard.Organization.Regions;
using API.Interfaces.Dashboard.Organization.Locations;
using API.Services.Dashboard.Organization.Locations;
using API.Interfaces.Dashboard.Organization.UnderDepartments;
using API.Services.Dashboard.Organization.UnderDepartments;
using API.Interfaces.Dashboard.Organization.Department;
using API.Services.Dashboard.Organization.Departments;
using API.Interfaces.Dashboard.Organization.Shifts;
using API.Services.Dashboard.Organization.Shifts;
using API.Interfaces.Dashboard.Organization.Branch;
using API.Services.Dashboard.Organization.Branch;
using API.Services.Dashboard.Organization;
using API.Interfaces.Dashboard.Organization;
// Build app
var builder = WebApplication.CreateBuilder(args);
// Bind JWT settings from appsettings.json
var jwtSettings = builder.Configuration.GetSection("Jwt").Get<JwtSettings>();
builder.Services.AddSingleton(jwtSettings);
// DbContext
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// Services
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<ICompanyService, CompanyService>();
builder.Services.AddScoped<IAdminService, AdminService>();
builder.Services.AddScoped<ISuperAdminCompanyService, SuperAdminCompanyService>();
builder.Services.AddScoped<IRegionService, RegionService>();
builder.Services.AddScoped<ILocationService, LocationService>();
builder.Services.AddScoped<IUnderDepartmentService, UnderDepartmentService>();
builder.Services.AddScoped<IDepartmentService, DepartmentService>();
builder.Services.AddScoped<IShiftService, ShiftService>();
builder.Services.AddScoped<IBranchService, BranchService>();
builder.Services.AddScoped<IBankService, BankService>();
builder.Services.AddSingleton(new JwtHelper(jwtSettings.Secret, jwtSettings.ExpiryMinutes));
// JWT Authentication
var key = Encoding.ASCII.GetBytes(jwtSettings.Secret);
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = jwtSettings.ValidateIssuer,
ValidateAudience = jwtSettings.ValidateAudience,
ValidateLifetime = jwtSettings.ValidateLifetime,
ValidateIssuerSigningKey = jwtSettings.ValidateIssuerSigningKey,
ValidIssuer = jwtSettings.Issuer,
ValidAudience = jwtSettings.Audience,
IssuerSigningKey = new SymmetricSecurityKey(key)
};
});
// Authorization policies
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("SuperAdminOnly", policy =>
policy.RequireClaim(ClaimTypes.Role, "1")); // RoleId = 1 → SuperAdmin
});
// Controllers & Swagger
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme. Just paste the token, 'Bearer ' will be added automatically.",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }
},
new string[] { }
}
});
});
// Build app
var app = builder.Build();
app.UseStaticFiles(); // Add before UseAuthorization()
// Swagger
app.UseSwagger();
app.UseSwaggerUI();
// Middleware
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();