87 lines
2.5 KiB
C#
87 lines
2.5 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
using WebVentaCoche.DataBase;
|
|
using WebVentaCoche.Helpers;
|
|
using WebVentaCoche.Models;
|
|
using WebVentaCoche.Services;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddControllersWithViews();
|
|
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
builder.Services.AddScoped<IUserHelper, UserHelper>();
|
|
builder.Services.AddTransient<SeedDb>();
|
|
builder.Services.AddIdentity<User, IdentityRole>(x =>
|
|
{
|
|
x.User.RequireUniqueEmail = true;
|
|
x.Password.RequireDigit = false;
|
|
x.Password.RequiredUniqueChars = 0;
|
|
x.Password.RequireLowercase = false;
|
|
x.Password.RequireNonAlphanumeric = false;
|
|
x.Password.RequireUppercase = false;
|
|
})
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(x => x.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWT:SecretToken"]!)),
|
|
ClockSkew = TimeSpan.Zero
|
|
});
|
|
|
|
builder.Services.AddScoped<IUserHelper, UserHelper>();
|
|
builder.Services.AddScoped<VerificationService>();
|
|
builder.Services.AddTransient<EmailService>();
|
|
|
|
builder.Services.AddMemoryCache();
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
SeedData(app);
|
|
|
|
void SeedData(WebApplication app)
|
|
{
|
|
IServiceScopeFactory? scopedFactory = app.Services.GetService<IServiceScopeFactory>();
|
|
|
|
using (IServiceScope? scope = scopedFactory!.CreateScope())
|
|
{
|
|
SeedDb? service = scope.ServiceProvider.GetService<SeedDb>();
|
|
service!.SeedAsync().Wait();
|
|
}
|
|
}
|
|
// Configure the HTTP request pipeline.
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Home/Error");
|
|
// 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.UseHttpsRedirection();
|
|
app.UseStaticFiles();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllerRoute(
|
|
name: "default",
|
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
|
|
|
app.Run();
|