98 lines
2.8 KiB
C#
98 lines
2.8 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Serilog;
|
|
using System.Text;
|
|
using WorkFlowCheck.Common.DTO;
|
|
using WorkFlowCheck.Web.Middleware;
|
|
using WorkFlowCheck.Web.Services;
|
|
using WorkFlowCheck.Web.Services.Interfaces;
|
|
using WorkFlowCheck.Web.Validators;
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
Log.Logger = new LoggerConfiguration()
|
|
.ReadFrom.Configuration(builder.Configuration)
|
|
.CreateLogger();
|
|
|
|
builder.Services.AddMemoryCache();
|
|
builder.Services.AddHttpContextAccessor();
|
|
builder.Services.AddHttpClient();
|
|
|
|
builder.Services.AddScoped<IUserService, UserService>();
|
|
builder.Services.AddScoped<IBaseStockService, BaseStockService>();
|
|
builder.Services.AddScoped<ICheckListService, CheckListService>();
|
|
|
|
builder.Host.UseSerilog();
|
|
// Add services to the container.
|
|
builder.Services.AddRazorPages();
|
|
|
|
builder.Services.AddAntiforgery(options =>
|
|
{
|
|
options.HeaderName = "X-CSRF-TOKEN"; // JS API hívásokhoz
|
|
});
|
|
|
|
var jwtSettings = builder.Configuration.GetSection("Jwt");
|
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.RequireHttpsMetadata = true;
|
|
options.SaveToken = true;
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = jwtSettings["Issuer"],
|
|
ValidAudience = jwtSettings["Audience"],
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["SecretKey"]))
|
|
};
|
|
});
|
|
|
|
builder.Services.AddFluentValidationSetup();
|
|
|
|
var app = builder.Build();
|
|
app.UseMiddleware<JwtMiddleware>();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/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.Use(async (context, next) =>
|
|
{
|
|
//if (string.IsNullOrEmpty(context.User?.Identity?.Name) &&
|
|
// context.Request.Path == "/")
|
|
//{
|
|
// context.Response.Redirect("/Account/Login");
|
|
// return;
|
|
//}
|
|
var isAuthenticated = context.User?.Identity?.IsAuthenticated == true;
|
|
var path = context.Request.Path.Value?.ToLower();
|
|
|
|
if (!isAuthenticated && !path.StartsWith("/account/login"))
|
|
{
|
|
context.Response.Redirect("/Account/Login");
|
|
return;
|
|
}
|
|
|
|
await next();
|
|
});
|
|
app.MapRazorPages();
|
|
|
|
app.Run();
|