Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
152 lines (124 loc) · 5.74 KB
/
Copy pathProgram.cs
File metadata and controls
152 lines (124 loc) · 5.74 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
140
141
142
143
144
145
146
147
148
149
150
151
152
using BlazorInventario.Components;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using System.Security.Claims;
using BlazorInventario.Data;
using BlazorInventario.Repositories;
using BlazorInventario.Services;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.AspNetCore.Components.Authorization;
using System.Globalization;
using QuestPDF.Infrastructure;
// QuestPDF license configuration
QuestPDF.Settings.License = LicenseType.Community;
var builder = WebApplication.CreateBuilder(args);
// Location
var cultureInfo = new CultureInfo("es-MX");
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? "Server=localhost;Database=mininventary;User=root;Password=pass;";
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// HTTP client for calling sign-in/sign-out endpoints from components
builder.Services.AddHttpClient();
// Register server authentication state provider so Blazor interactive components
// can receive the authentication state via CascadingAuthenticationState.
builder.Services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
// DB connection factory and repositories/services
builder.Services.AddSingleton<IDbConnectionFactory>(new MySqlConnectionFactory(connectionString));
// User repository registration
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<ICategoriesRepository, CategoriesRepository>();
builder.Services.AddScoped<IProductsRepository, ProductsRepository>();
builder.Services.AddScoped<IMovementsRepository, MovementsRepository>();
builder.Services.AddScoped<IInventoryService, InventoryService>();
// Allow services to access current HttpContext for server-side role checks
builder.Services.AddHttpContextAccessor();
// Cookie authentication
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/login";
options.LogoutPath = "/signout";
});
builder.Services.AddAuthorization();
// Provide the Task<AuthenticationState> cascading parameter for AuthorizeView and related components
builder.Services.AddCascadingAuthenticationState();
// Export service for movements CSV
builder.Services.AddScoped<BlazorInventario.Services.IMovementsExportService, BlazorInventario.Services.MovementsExportService>();
// Export services for products and categories
builder.Services.AddScoped<BlazorInventario.Services.IProductsExportService, BlazorInventario.Services.ProductsExportService>();
builder.Services.AddScoped<BlazorInventario.Services.ICategoriesExportService, BlazorInventario.Services.CategoriesExportService>();
// Users export
builder.Services.AddScoped<BlazorInventario.Services.IUsersExportService, BlazorInventario.Services.UsersExportService>();
// Kardex export
builder.Services.AddScoped<BlazorInventario.Services.IKardexExportService, BlazorInventario.Services.KardexExportService>();
// Kardex PDF export
builder.Services.AddScoped<BlazorInventario.Services.IKardexPdfExportService, BlazorInventario.Services.KardexPdfExportService>();
// Application build
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
app.UseHttpsRedirection();
app.UseAntiforgery();
// Localización
app.UseRequestLocalization(options =>
{
var supportedCultures = new[] { new CultureInfo("es-MX") };
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("es-MX");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
// Authentication / Authorization
app.UseAuthentication();
app.UseAuthorization();
// Sign-in endpoint
app.MapPost("/signin", async (HttpContext http, IAuthService authService) =>
{
var req = http.Request;
var form = await req.ReadFormAsync();
var username = form["username"].ToString();
var password = form["password"].ToString();
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
return Results.Redirect("/login?failed=1");
}
var user = await authService.ValidateCredentialsAsync(username, password);
if (user is null)
{
return Results.Redirect("/login?failed=1");
}
var claims = new[]
{
new Claim(ClaimTypes.NameIdentifier, user.id.ToString()),
new Claim(ClaimTypes.Name, user.name ?? user.email ?? string.Empty),
new Claim(ClaimTypes.Email, user.email ?? string.Empty),
};
if (!string.IsNullOrEmpty(user.role))
{
claims = claims.Append(new Claim(ClaimTypes.Role, user.role)).ToArray();
}
var id = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(id);
await http.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
return Results.Redirect("/");
});
app.MapGet("/signout", async (HttpContext http) =>
{
await http.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return Results.Redirect("/");
});
app.MapStaticAssets();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();