premik na samo program.cs
This commit is contained in:
@@ -1,16 +1,31 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog;
|
||||
using NLog.Web;
|
||||
using ProjecThing.Data;
|
||||
using ProjecThing.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Security;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
using NLog.Web;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace ProjecThing
|
||||
{
|
||||
@@ -21,35 +36,223 @@ namespace ProjecThing
|
||||
//var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
|
||||
//logger.Debug("INIT");
|
||||
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Configure NLog
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddConsole();
|
||||
builder.Host.UseNLog();
|
||||
|
||||
builder.Services.Configure<CookiePolicyOptions>(options =>
|
||||
{
|
||||
options.CheckConsentNeeded = context => true;
|
||||
options.MinimumSameSitePolicy = SameSiteMode.None;
|
||||
});
|
||||
|
||||
//builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
||||
//{
|
||||
// options.KnownProxies.Add(IPAddress.Parse("192.168.111.78"));
|
||||
//});
|
||||
|
||||
// Database connection
|
||||
string connectionString = builder.Configuration.GetConnectionString("DataConnection");
|
||||
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
|
||||
//builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DataConnection")));
|
||||
|
||||
// Session
|
||||
builder.Services.AddSession(options =>
|
||||
{
|
||||
options.IdleTimeout = TimeSpan.FromHours(3);
|
||||
options.Cookie.HttpOnly = true;
|
||||
options.Cookie.IsEssential = true;
|
||||
options.Cookie.MaxAge = TimeSpan.FromHours(3);
|
||||
});
|
||||
|
||||
// JWT
|
||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
|
||||
{
|
||||
options.SaveToken = true;
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = "ProjecThing",
|
||||
ValidAudience = "Android",
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("!Appli$cation#9999#!"))
|
||||
};
|
||||
});
|
||||
|
||||
// Identity
|
||||
builder.Services.AddIdentity<IdentityApplicationUser, IdentityApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
|
||||
|
||||
builder.Services.AddRazorPages(options =>
|
||||
{
|
||||
//options.Conventions.AddPageRoute("/Administration/Users", "/AdministrationUsers");
|
||||
}).AddRazorRuntimeCompilation();
|
||||
|
||||
//builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
||||
//{
|
||||
// options.ForwardedHeaders =
|
||||
// ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
||||
// options.KnownProxies.Add(IPAddress.Parse("192.168.111.78"));
|
||||
// //options.ForwardedForHeaderName = "X-Forwarded-For-My-Custom-Header-Name";
|
||||
//});
|
||||
|
||||
builder.Services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
|
||||
|
||||
builder.Services.Configure<IdentityOptions>(options =>
|
||||
{
|
||||
// Password settings
|
||||
options.Password.RequireDigit = true;
|
||||
options.Password.RequireLowercase = true;
|
||||
options.Password.RequireNonAlphanumeric = true;
|
||||
options.Password.RequireUppercase = true;
|
||||
options.Password.RequiredLength = 8;
|
||||
options.Password.RequiredUniqueChars = 1;
|
||||
|
||||
// Lockout settings
|
||||
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
|
||||
options.Lockout.MaxFailedAccessAttempts = 3;
|
||||
options.Lockout.AllowedForNewUsers = true;
|
||||
|
||||
// User settings
|
||||
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
options.User.RequireUniqueEmail = true;
|
||||
});
|
||||
|
||||
builder.Services.ConfigureApplicationCookie(options =>
|
||||
{
|
||||
// Cookie
|
||||
options.Cookie.MaxAge = TimeSpan.FromHours(3);
|
||||
options.Cookie.HttpOnly = true;
|
||||
options.SlidingExpiration = true;
|
||||
options.Cookie.SecurePolicy = CookieSecurePolicy.None;//Samo unencripted na locas
|
||||
|
||||
options.LoginPath = $"/User/Login";
|
||||
options.LogoutPath = $"/User/Logout";
|
||||
options.AccessDeniedPath = $"/";
|
||||
options.ExpireTimeSpan = TimeSpan.FromHours(3);//Povecano na 15min
|
||||
});
|
||||
|
||||
builder.Services.AddDistributedMemoryCache();
|
||||
|
||||
builder.Services.Configure<IISOptions>(options =>
|
||||
{
|
||||
options.AutomaticAuthentication = false;
|
||||
});
|
||||
|
||||
//builder.Services.AddDataProtection().SetApplicationName("ProjecThing").PersistKeysToFileSystem(new DirectoryInfo(@"Keys/"));
|
||||
|
||||
builder.Services.AddDataProtection()
|
||||
.SetApplicationName("ProjecThing")
|
||||
.PersistKeysToDbContext<ApplicationDbContext>()
|
||||
.AddKeyManagementOptions(options =>
|
||||
{
|
||||
options.NewKeyLifetime = new TimeSpan(365, 0, 0, 0);
|
||||
options.AutoGenerateKeys = true;
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Apply migrations in production
|
||||
#if !DEBUG
|
||||
ApplyMigrations(app);
|
||||
#endif
|
||||
|
||||
// ===== Configure Middleware Pipeline =====
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
//app.UseForwardedHeaders();
|
||||
}
|
||||
else
|
||||
{
|
||||
app.UseExceptionHandler("/Error");
|
||||
//app.UseForwardedHeaders();
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
//app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||
//{
|
||||
// ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||
//});
|
||||
|
||||
|
||||
var defaultCulture = new CultureInfo("sl-SI");
|
||||
var localizationOptions = new RequestLocalizationOptions
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture(defaultCulture),
|
||||
SupportedCultures = new List<CultureInfo> { defaultCulture },
|
||||
SupportedUICultures = new List<CultureInfo> { defaultCulture }
|
||||
};
|
||||
app.UseRequestLocalization(localizationOptions);
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
app.UseStaticFiles();
|
||||
app.UseCookiePolicy();
|
||||
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseSession();
|
||||
|
||||
app.MapRazorPages();
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
|
||||
//CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(
|
||||
webBuilder =>
|
||||
private static void ApplyMigrations(IHost app)
|
||||
{
|
||||
using var scope = app.Services.CreateScope();
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
|
||||
// Check and apply pending migrations
|
||||
var pendingMigrations = dbContext.Database.GetPendingMigrations();
|
||||
if (pendingMigrations.Any())
|
||||
{
|
||||
webBuilder.ConfigureLogging(logging =>
|
||||
{
|
||||
logging.ClearProviders();
|
||||
logging.AddConsole();
|
||||
}).UseNLog();
|
||||
Console.WriteLine("Applying pending migrations...");
|
||||
dbContext.Database.Migrate();
|
||||
Console.WriteLine("Migrations applied successfully.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("No pending migrations found.");
|
||||
}
|
||||
}
|
||||
|
||||
#if !DEBUG
|
||||
webBuilder.UseKestrel(opts =>
|
||||
{
|
||||
var appServices = opts.ApplicationServices;
|
||||
//opts.Listen(IPAddress.Parse("192.168.1.150"), 80);
|
||||
//opts.Listen(IPAddress.Parse("192.168.1.150"), 443, o => o.UseHttps(h => { h.UseLettuceEncrypt(appServices); }));
|
||||
// public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(
|
||||
// webBuilder =>
|
||||
// {
|
||||
// webBuilder.ConfigureLogging(logging =>
|
||||
// {
|
||||
// logging.ClearProviders();
|
||||
// logging.AddConsole();
|
||||
// }).UseNLog();
|
||||
|
||||
//opts.Listen(IPAddress.Parse("192.168.111.77"), 5005);
|
||||
opts.Listen(IPAddress.Parse("192.168.178.205"), 5005);//novi server
|
||||
opts.Listen(IPAddress.Loopback, port: 8081);
|
||||
//opts.ListenLocalhost(4433, opts => opts.UseHttps());
|
||||
opts.ListenLocalhost(5005);
|
||||
//opts.ListenLocalhost(5005, opts => opts.UseHttps());
|
||||
});
|
||||
#endif
|
||||
webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
//#if !DEBUG
|
||||
// webBuilder.UseKestrel(opts =>
|
||||
// {
|
||||
// var appServices = opts.ApplicationServices;
|
||||
// //opts.Listen(IPAddress.Parse("192.168.1.150"), 80);
|
||||
// //opts.Listen(IPAddress.Parse("192.168.1.150"), 443, o => o.UseHttps(h => { h.UseLettuceEncrypt(appServices); }));
|
||||
|
||||
// //opts.Listen(IPAddress.Parse("192.168.111.77"), 5005);
|
||||
// opts.Listen(IPAddress.Parse("192.168.178.205"), 5005);//novi server
|
||||
// opts.Listen(IPAddress.Loopback, port: 8081);
|
||||
// //opts.ListenLocalhost(4433, opts => opts.UseHttps());
|
||||
// opts.ListenLocalhost(5005);
|
||||
// //opts.ListenLocalhost(5005, opts => opts.UseHttps());
|
||||
// });
|
||||
//#endif
|
||||
// webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
|
||||
// webBuilder.UseStartup<Startup>();
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user