-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (83 loc) · 3.13 KB
/
Program.cs
File metadata and controls
96 lines (83 loc) · 3.13 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
using Box.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Authentication.Cookies;
using Authentication;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using Microsoft.EntityFrameworkCore;
using Data;
using Data.Context;
using Data.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
#region Configuracion de la base de datos SQLite
builder.Services.AddDbContext<MyDbContext>(options =>
options.UseSqlite("Data Source=MyDB.sqlite"));
builder.Services.AddScoped<IMyDbContext, MyDbContext>();
#endregion
#region Servicios
builder.Services.AddScoped<IFacturaServices,FacturaServices>();
builder.Services.AddScoped<IProductoServices,ProductoServices>();
builder.Services.AddScoped<IProveedorServices,ProveedorServices>();
builder.Services.AddScoped<ICategoriaServices,CategoriaServices>();
builder.Services.AddScoped<IClienteServices, ClienteServices>();
builder.Services.AddScoped<IPagoServices, PagoServices>();
builder.Services.AddScoped<ICuadrarCajaServices, CuadrarCajaServices>();
builder.Services.AddScoped<IUsuarioServices, UsuarioServices>();
#endregion
#region Authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.LoginPath = "/"; // Ruta a tu página de login
// options.AccessDeniedPath = "/"; // Ruta opcional para acceso denegado
});
builder.Services.AddAuthorization();
// builder.Services.AddAuthorization(options =>
// {
// options.AddPolicy("AuthenticatedUser", policy => policy.RequireAuthenticatedUser());
// });
builder.Services.AddAuthenticationCore();
builder.Services.AddScoped<ProtectedSessionStorage>();
builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthenticationStateProvider>();
//builder.Services.AddSingleton<UserAccountService>();
builder.Services.AddScoped<IUserAccountService,UserAccountService>();
#endregion
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}
app.UseStaticFiles();
app.UseAntiforgery();
// Asegúrate de que estos middleware estén en este orden exacto
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
var scopeFactory = app.Services.GetRequiredService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<MyDbContext>();
try
{
if (db.Database.EnsureCreated())
{
// La base de datos se ha creado (o ya existe)
}
}
catch (Exception ex)
{
Console.WriteLine($"Error al crear la base de datos: {ex.Message}");
// Puedes agregar m�s manejo de errores seg�n tus necesidades
}
}
app.Run();