-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
108 lines (81 loc) · 3.41 KB
/
Program.cs
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
using EFS_23298_23327.Data;
using EFS_23298_23327.Hubs;
using EFS_23298_23327.Models;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Configuration;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddCors(options => {
options.AddPolicy("AllowAll",
builder => {
builder.WithOrigins("http://localhost:3000")
.AllowCredentials()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddSignalR();
builder.Services.AddTransient<IEmailSender, EnviaEmail>(i =>
new EnviaEmail(
builder.Configuration["EnviaEmail:Host"],
int.Parse(builder.Configuration["EnviaEmail:Port"]),
bool.Parse(builder.Configuration["EnviaEmail:EnableSSL"]),
builder.Configuration["EnviaEmail:Username"],
builder.Configuration["EnviaEmail:Password"]
)
);
builder.Services.AddDefaultIdentity<Utilizadores>(options => options.SignIn.RequireConfirmedAccount = bool.Parse(builder.Configuration["RequirePasswordLogin:RequirePassword"]))
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>().AddErrorDescriber<ErrosIdentityUser>().AddApiEndpoints();
builder.Services.AddControllersWithViews();
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
builder.Services.ConfigureApplicationCookie(options =>
{
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always; // Use apenas em HTTPS
options.Cookie.SameSite = SameSiteMode.None; // Permite cross-site
options.Cookie.Name = "MyAuthCookie"; // Nome do cookie
options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
options.SlidingExpiration = true;
});
var app = builder.Build();
app.UseCors("AllowAll");
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
app.UseItToSeedSqlServer();
}
else
{
app.UseStatusCodePagesWithReExecute("/Home/Error", "?statusCode={0}");
app.UseItToSeedSqlServer();
// 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.MapIdentityApi<Utilizadores>();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.MapGroup("/api").MapIdentityApi<Utilizadores>();
app.UseAuthorization();
app.MapControllerRoute(
name: "Gerir",
pattern: "{area:exists}/{controller=Temas}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
//Redireciona para paginas criadas,com o código do erro
app.UseStatusCodePagesWithReExecute("/Home/Error", "?statusCode={0}");
app.MapHub<ClassHub>("/hub");
app.MapRazorPages();
app.Run();