-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartup.cs
More file actions
139 lines (123 loc) · 5.71 KB
/
Copy pathStartup.cs
File metadata and controls
139 lines (123 loc) · 5.71 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
133
134
135
136
137
138
139
using eTickets.Data;
using Gate_Pass_management.Data;
using Gate_Pass_management.Models;
using Gate_Pass_management.Infrastructure.Sqlite;
using FluentValidation;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; // ← keep this at the top
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OfficeOpenXml;
using Microsoft.Extensions.FileProviders;
using System.IO;
using Gate_Pass_management.Services;
using Gate_Pass_management.Hubs;
namespace Gate_Pass_management
{
public class Startup
{
public Startup(IConfiguration configuration)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// SQLite database for local development with pragma interceptor
services.AddDbContext<AppDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnectionString"))
.AddInterceptors(new SqlitePragmaInterceptor()));
// Identity (roles + 2FA ready)
services
.AddDefaultIdentity<AppUser>(options =>
{
options.SignIn.RequireConfirmedAccount = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false; // relax for dev
// Lockout policy
options.Lockout.AllowedForNewUsers = true;
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
// 2FA tokens
options.Tokens.AuthenticatorTokenProvider = TokenOptions.DefaultAuthenticatorProvider;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>();
// Configure cookies explicitly
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.AccessDeniedPath = "/Account/AccessDenied";
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromDays(7);
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
options.Cookie.SameSite = SameSiteMode.Lax;
});
services.AddControllersWithViews();
services.AddRazorPages();
// SignalR for real-time updates
services.AddSignalR();
// Authorization policies for Gate Pass
services.AddAuthorization(options =>
{
options.AddPolicy("SecurityGuard", policy => policy.RequireRole("SecurityGuard", "Admin"));
options.AddPolicy("Reception", policy => policy.RequireRole("Reception", "Admin"));
options.AddPolicy("Employee", policy => policy.RequireRole("Employee", "Manager", "Admin"));
options.AddPolicy("Admin", policy => policy.RequireRole("Admin"));
});
// Existing services
services.AddScoped<IGatePassService, GatePassService>();
services.AddScoped<IVisitorMetricsService, VisitorMetricsService>();
services.AddScoped<ISchedulingService, SchedulingService>();
services.AddScoped<ISmsSender, TwilioSmsSender>();
services.AddScoped<IAuditService, AuditService>();
// Enhanced Gate Pass Workflow services
services.AddScoped<IGatePassWorkflowService, GatePassWorkflowService>();
services.AddScoped<IPdfGenerationService, PdfGenerationService>();
services.AddScoped<IVisitorTrackingNotifier, VisitorTrackingNotifier>();
services.AddHttpContextAccessor();
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
// Serve files from the project's /assets directory at the /assets URL path
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "assets")),
RequestPath = "/assets"
});
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapControllers(); // Enable API controllers
endpoints.MapHub<VisitorMetricsHub>("/hubs/visitorMetrics");
endpoints.MapHub<VisitorTrackingHub>("/hubs/visitorTracking");
endpoints.MapHub<SchedulerHub>("/hubs/scheduler");
});
// Seed the database with initial data
AppDbInitializer.Seed(app);
}
}
}