-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
109 lines (93 loc) · 3.16 KB
/
Copy pathProgram.cs
File metadata and controls
109 lines (93 loc) · 3.16 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
using FitEasy.API.Data;
using FitEasy.API.Interfaces.Login;
using FitEasy.API.Interfaces.Security;
using FitEasy.API.Repositories.Login;
using FitEasy.API.Services.Login;
using FitEasy.API.Services.Security;
using FitEasy.API.Settings;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// DbContext
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
// DI
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddSingleton<IPasswordEncryptor>(sp =>
{
var config = sp.GetRequiredService<IConfiguration>();
var aes = config.GetSection("Aes");
var key = aes["Key"];
var iv = aes["IV"];
if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(iv))
throw new InvalidOperationException("As configurações 'Aes:Key' e 'Aes:IV' são obrigatórias.");
return new AesPasswordEncryptor(key, iv);
});
builder.Services.AddScoped<ILoginService, LoginService>();
builder.Services.Configure<JwtSettings>(builder.Configuration.GetSection("Jwt"));
var jwtSection = builder.Configuration.GetSection("Jwt");
var key = Encoding.UTF8.GetBytes(jwtSection["Key"]!);
builder.Services
.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(opt =>
{
opt.RequireHttpsMetadata = false;
opt.SaveToken = true;
opt.TokenValidationParameters = new()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtSection["Issuer"],
ValidAudience = jwtSection["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(key),
ClockSkew = TimeSpan.Zero
};
});
builder.Services.AddAuthorization();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAngularOrigin", policy =>
{
policy.WithOrigins("http://localhost:4200", "http://100.79.151.95:4200", "http://100.79.151.95")
.AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Version = "v1",
Title = "FitEasy.API",
Description = "API para FitEasy",
});
c.EnableAnnotations();
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "FitEasy.API v1");
});
}
//app.UseHttpsRedirection();
// Se não tiver HTTPS configurado, pode comentar a linha acima temporariamente.
// app.UseHttpsRedirection();
app.UseCors("AllowAngularOrigin");
app.UseAuthentication();
app.UseAuthorization();
// LINHA CRÍTICA QUE FALTAVA
app.MapControllers();
app.Run();