Prvi commit
This commit is contained in:
197
EveryThing/Startup.cs
Normal file
197
EveryThing/Startup.cs
Normal file
@@ -0,0 +1,197 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using EveryThing.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Connections;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using EveryThing.Data;
|
||||
using Microsoft.AspNetCore.Authentication.Negotiate;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
|
||||
namespace EveryThing
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.Configure<CookiePolicyOptions>(options =>
|
||||
{
|
||||
options.CheckConsentNeeded = context => true;
|
||||
options.MinimumSameSitePolicy = SameSiteMode.None;
|
||||
});
|
||||
|
||||
//services.Configure<ForwardedHeadersOptions>(options =>
|
||||
//{
|
||||
// options.KnownProxies.Add(IPAddress.Parse("192.168.111.78"));
|
||||
//});
|
||||
|
||||
// Database connection
|
||||
string connectionString = Configuration.GetConnectionString("DataConnection");
|
||||
services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
|
||||
//services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DataConnection")));
|
||||
|
||||
// Session
|
||||
services.AddSession(options =>
|
||||
{
|
||||
options.IdleTimeout = TimeSpan.FromMinutes(15);
|
||||
options.Cookie.HttpOnly = true;
|
||||
options.Cookie.IsEssential = true;
|
||||
options.Cookie.MaxAge = TimeSpan.FromHours(3);
|
||||
});
|
||||
|
||||
// JWT
|
||||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
|
||||
{
|
||||
options.SaveToken = true;
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = "EveryThing",
|
||||
ValidAudience = "Android",
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("!Appli$cation#9999#!"))
|
||||
};
|
||||
});
|
||||
|
||||
// Identity
|
||||
services.AddIdentity<IdentityApplicationUser, IdentityApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
|
||||
|
||||
services.AddRazorPages(options =>
|
||||
{
|
||||
//options.Conventions.AddPageRoute("/Administration/Users", "/AdministrationUsers");
|
||||
}).AddRazorRuntimeCompilation();
|
||||
|
||||
//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";
|
||||
//});
|
||||
|
||||
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
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.FromMinutes(15);//Povecano na 15min
|
||||
});
|
||||
|
||||
services.AddDistributedMemoryCache();
|
||||
|
||||
services.Configure<IISOptions>(options =>
|
||||
{
|
||||
options.AutomaticAuthentication = false;
|
||||
});
|
||||
|
||||
services.AddDataProtection().SetApplicationName("EveryThing").PersistKeysToFileSystem(new DirectoryInfo(@"Keys/"));
|
||||
|
||||
//services.AddHttpsRedirection(options =>
|
||||
//{
|
||||
// options.RedirectStatusCode = (int)HttpStatusCode.PermanentRedirect;
|
||||
// options.HttpsPort = 443;
|
||||
//});
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.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.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapRazorPages();
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user