This repository has been archived on 2023-10-28. You can view files and clone it, but cannot push or open issues or pull requests.
pwt-0x01-ng/Startup.cs
surtur 24a830b523
merge: feature-add-identity
commit 47c8a3e8e27fb8a71e702232c681d588fa152bc0
Author: surtur <a_mirre@utb.cz>
Date:   Thu Jan 28 14:58:07 2021 +0100

    chore: register and login on the right

commit 3e32cd61c2436123887d78f4ae396577b378e53c
Author: surtur <a_mirre@utb.cz>
Date:   Thu Jan 28 14:43:07 2021 +0100

    chore: add {Login,Register}ViewModel

commit f82f12a5328f1500c3a58dccb3728dc86d1cc92c
Author: surtur <a_mirre@utb.cz>
Date:   Thu Jan 28 14:30:36 2021 +0100

    chore: libs and pkgs version bump

commit 202b06505c94817c7bc60b432a9320efce80afac
Author: surtur <a_mirre@utb.cz>
Date:   Thu Jan 28 14:29:40 2021 +0100

    chore: local post-kaniko convenience chown

    * [skip ci]

commit 7e4c4ccbbf4f7290d47c4c682f09ee508b34ae1e
Author: surtur <a_mirre@utb.cz>
Date:   Thu Jan 28 14:29:15 2021 +0100

    chore: add Manager on app start

commit 62e9d567c07794b821eb86e7dcff4ecbb6bbd47e
Author: surtur <a_mirre@utb.cz>
Date:   Thu Jan 28 14:12:24 2021 +0100

    chore: add Login page

    * include FontAwesome (atm from CDN, will fetch it later and bundle it
      here)

commit 3c899a2d024d8b74c7e28dfd4b86e1332d964129
Author: surtur <a_mirre@utb.cz>
Date:   Thu Jan 28 13:33:32 2021 +0100

    fix: use proper service provider

commit 88a6e0f45eae7d71cf352d01a693429246effd98
Author: surtur <a_mirre@utb.cz>
Date:   Thu Jan 28 12:26:41 2021 +0100

    chore: do creation async + s/FirstName/Name/

commit eaed0ec13c0dd3824802a698ac9ed30bffc52de9
Author: surtur <a_mirre@utb.cz>
Date:   Thu Jan 28 10:55:23 2021 +0100

    fix: err creating admin

    * cause of silly passwd requirements - require {upper,lower}case and
      non-alphanumeric characters is implicitly set to true

commit f55f8517f8f8516192b5d156c71596e7fcc55ad2
Author: surtur <a_mirre@utb.cz>
Date:   Wed Jan 27 23:11:06 2021 +0100

    feat: table renames+call role-creating methods

    * no "AspNet" prefix
    * actually call Ensure{Admin,Roles}Created on app start

commit 20d3458f281ec9478dc9b0fab3795556dcd0a78f
Author: surtur <a_mirre@utb.cz>
Date:   Wed Jan 27 22:05:11 2021 +0100

    fix typo: DbContext --> DBContext

commit f972ac83e7197813bf4c9b4d9bb44d640b5dd071
Author: surtur <a_mirre@utb.cz>
Date:   Wed Jan 27 20:55:22 2021 +0100

    chore: add login and usr mgmt logic

    * add login and registration views
    * add methods to create admin user on app start

commit b65a6d21f8dc5227a8da2d5040348e269c2f3711
Author: surtur <a_mirre@utb.cz>
Date:   Wed Jan 27 16:02:33 2021 +0100

    chore: add auth options

    * FIXME dev fun settings need to be replaced with proper settings

commit 8f84939c22ec96e0a143350b9b9ba3d738dfc149
Author: surtur <a_mirre@utb.cz>
Date:   Wed Jan 27 14:54:57 2021 +0100

    chore: adding identity roles (batch 1)

    * preparing Startup and DBContext to work with roles and auth
    * added Roles enum
    * added User model
    * added Microsoft.AspNetCore.Identity.EntityFrameworkCore v3.1.10 nuget

    * note: auth is not ready and working yet, this is batch one of the
      preparations
2021-01-28 15:27:25 +01:00

128 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.CookiePolicy;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using pwt_0x01_ng.Models.Identity;
using pwt_0x01_ng.Models.Database;
using Microsoft.EntityFrameworkCore;
namespace pwt_0x01_ng
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.Lax;
options.HttpOnly = HttpOnlyPolicy.Always;
options.Secure = CookieSecurePolicy.Always;
});
services.AddControllersWithViews();
services.AddRazorPages();
var connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");
services.AddDbContext<DBContext>(options =>
options.UseNpgsql(
connectionString
)
);
services.AddIdentity<User, Role>().AddEntityFrameworkStores<DBContext>().AddDefaultTokenProviders();
services.Configure<IdentityOptions>(o =>
{
/* dev fun settings */
o.Password.RequireDigit = false;
o.Password.RequireUppercase = false;
o.Password.RequireLowercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequiredLength = 2;
o.Password.RequiredUniqueChars = 1;
o.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(1);
o.Lockout.MaxFailedAccessAttempts = 3;
o.Lockout.AllowedForNewUsers = false;
o.User.RequireUniqueEmail = true;
/* FIXME
* o.Password.RequireDigit = true;
* o.Password.RequiredLength = 18;
* o.Password.RequiredUniqueChars = 10;
* o.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
* o.Lockout.MaxFailedAccessAttempts = 3;
* o.Lockout.AllowedForNewUsers = true;
* o.User.RequireUniqueEmail = true;
*/
});
services.ConfigureApplicationCookie(o => {
o.Cookie.HttpOnly = true;
o.ExpireTimeSpan = TimeSpan.FromDays(2);
o.SlidingExpiration = true;
o.LoginPath = "/Security/Account/Login";
o.LogoutPath = "/Security/Account/Logout";
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddResponseCompression();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.EnvironmentName.Equals("Development"))
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStatusCodePages();
app.UseStatusCodePagesWithReExecute("/Home/HandleError/{0}");
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Products}/{action=Details}/{id?}"
);
});
app.UseStaticFiles();
app.UseCookiePolicy();
}
}
}