150 lines
4.8 KiB
C#
150 lines
4.8 KiB
C#
using Core.Interfaces;
|
|
using Core.Services;
|
|
using Google.Authenticator;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using Models.Interfaces;
|
|
using Models.Models;
|
|
using Models.Repositories;
|
|
using phronCare.API.Models;
|
|
using Services.Models;
|
|
using Services.Services;
|
|
using Services.Interfaces;
|
|
using System.Text;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
// Asegúrate de que el archivo appsettings.json se cargue correctamente
|
|
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
|
|
|
#region DbContext Identity Configuration
|
|
var configuration = builder.Configuration;
|
|
builder.Services.AddDbContext<phronCareDbContext>(options => options.UseSqlServer(configuration.GetConnectionString("phronCareDB")));
|
|
builder.Services.AddDbContext<PhronCareOperationsHubContext>(options =>
|
|
options.UseSqlServer(configuration.GetConnectionString("PhronCareOperationsHubConnection")));
|
|
|
|
builder.Services.AddScoped<ITicketRepository, TicketRepository>();
|
|
builder.Services.AddScoped<ITicketDom, TicketService>();
|
|
|
|
#endregion
|
|
#region Require Confirmed Email
|
|
builder.Services.Configure<IdentityOptions>(
|
|
opts => opts.SignIn.RequireConfirmedEmail = true
|
|
);
|
|
#endregion
|
|
#region Identity EF Configuration
|
|
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
|
|
.AddEntityFrameworkStores<phronCareDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
#endregion
|
|
|
|
builder.Services.Configure<DataProtectionTokenProviderOptions>( opts => opts.TokenLifespan=TimeSpan.FromHours(10));
|
|
builder.Services.AddSingleton<TwoFactorAuthenticator>();
|
|
|
|
#region Authentication Service
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
}).AddJwtBearer(options =>
|
|
{
|
|
options.Audience = configuration["JWT:ValidAudience"];
|
|
options.RequireHttpsMetadata = false;
|
|
options.SaveToken = true;
|
|
options.IncludeErrorDetails = true;
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = configuration["JWT:ValidIssuer"],
|
|
ValidAudience = configuration["JWT:ValidAudience"],
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(configuration["JWT:Secret"])),
|
|
};
|
|
});
|
|
#endregion
|
|
|
|
#region Email Configuration
|
|
var emailConfig = configuration.GetSection("EmailConfiguration").Get<EmailConfiguration>();
|
|
if (emailConfig == null)
|
|
{
|
|
throw new InvalidOperationException("La configuración del correo electrónico no se encontró o es inválida.");
|
|
}
|
|
builder.Services.AddSingleton(emailConfig);
|
|
builder.Services.AddScoped<IEmailService, EmailService>();
|
|
#endregion
|
|
|
|
#region Swagger Authorization
|
|
builder.Services.AddSwaggerGen(option =>
|
|
{
|
|
option.SwaggerDoc("v1", new OpenApiInfo { Title = "phronCARE API - SaludLAB", Version = "v1" });
|
|
option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
In = ParameterLocation.Header,
|
|
Description = "Por favor, ingrese un token valido",
|
|
Name = "Authorization",
|
|
Type = SecuritySchemeType.Http,
|
|
BearerFormat = "JWT",
|
|
Scheme = "Bearer"
|
|
});
|
|
option.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type=ReferenceType.SecurityScheme,
|
|
Id="Bearer"
|
|
}
|
|
},
|
|
new string[]{}
|
|
}
|
|
});
|
|
});
|
|
#endregion
|
|
|
|
#region CORS
|
|
// builder.Services.AddCors(p => p.AddPolicy("CORS", builder =>
|
|
// {
|
|
// builder
|
|
// .AllowAnyOrigin()
|
|
// .AllowAnyMethod()
|
|
// .AllowAnyHeader();
|
|
// }));
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("CORS", policy =>
|
|
{
|
|
policy
|
|
.WithOrigins("http://ui.biodec.saludlab.com.ar", "http://phroncareUI:80", "http://192.168.10.110:9002")
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
#endregion
|
|
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
var app = builder.Build();
|
|
|
|
//if (app.Environment.IsDevelopment())
|
|
//{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
//}
|
|
|
|
app.UseCors("CORS");
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.UseHttpsRedirection();
|
|
app.MapControllers();
|
|
app.Run(); |