72 lines
1.7 KiB
C#
72 lines
1.7 KiB
C#
using System.Text;
|
|
using line_gestao_api.Data;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
// ? CORS (Angular)
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("Front", p =>
|
|
p.WithOrigins("http://localhost:4200")
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
);
|
|
});
|
|
|
|
// EF Core (PostgreSQL)
|
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("Default"))
|
|
);
|
|
|
|
// Swagger
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
// JWT
|
|
var jwtKey = builder.Configuration["Jwt:Key"]!;
|
|
var issuer = builder.Configuration["Jwt:Issuer"];
|
|
var audience = builder.Configuration["Jwt:Audience"];
|
|
|
|
builder.Services
|
|
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = issuer,
|
|
ValidAudience = audience,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
|
|
};
|
|
});
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
var app = builder.Build();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
// ? CORS precisa vir antes de Auth/Authorization
|
|
app.UseCors("Front");
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|