Commit 0e3bc055 authored by Brian Parks's avatar Brian Parks
Browse files

Merge branch 'dev/bparks/12-authentication' into 'master'

Dev/bparks/12 authentication

Closes #12

See merge request !7
parents d608b1cc 0ce18af5
Pipeline #1570 passed with stages
in 1 minute and 20 seconds
create table Users
(
Id integer not null auto_increment primary key,
LockoutEnd datetime null,
TwoFactorEnabled bit(1) not null,
PhoneNumberConfirmed bit(1) not null,
PhoneNumber varchar(32),
ConcurrencyStamp varchar(255),
SecurityStamp varchar(255),
PasswordHash varchar(255),
EmailConfirmed bit(1) not null,
NormalizedEmail varchar(255),
Email varchar(255),
NormalizedUserName varchar(255),
UserName varchar(255),
LockoutEnabled bit(1) not null,
AccessFailedCount integer
);
\ No newline at end of file
create table Claims
(
Id integer not null auto_increment primary key,
UserId integer not null,
ClaimType varchar(255),
ClaimValue text
);
\ No newline at end of file
......@@ -23,6 +23,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DB", "DB", "{50696695-8076-
2020-03-15-02-add-last-checked-field.sql = 2020-03-15-02-add-last-checked-field.sql
2020-03-16-01-add-other-status-fields.sql = 2020-03-16-01-add-other-status-fields.sql
2020-03-17-01-add-status-record-table.sql = 2020-03-17-01-add-status-record-table.sql
2020-03-19-01-add-users-table.sql = 2020-03-19-01-add-users-table.sql
2020-03-19-02-add-claims-table.sql = 2020-03-19-02-add-claims-table.sql
EndProjectSection
EndProject
Global
......
......@@ -8,6 +8,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using Microsoft.AspNetCore.Identity;
namespace Monocle.Web
{
......@@ -24,6 +25,10 @@ namespace Monocle.Web
public DbSet<Service> Services { get; set; } = null!;
public DbSet<StatusRecord> StatusRecords { get; set; } = null!;
// Identity
public DbSet<IdentityUser<int>> Users { get; set; } = null!;
public DbSet<IdentityUserClaim<int>> Claims { get; set; } = null!;
private void OnEntityTracked(object? sender, EntityTrackedEventArgs e)
{
if (e.Entry.Entity is IEntityKeyed keyedEntity && string.IsNullOrWhiteSpace(keyedEntity.Key))
......
@page
@model Monocle.Web.Areas.Identity.Pages.Account.RegisterModel
@{
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Monocle.Web.Areas.Identity.Pages.Account
{
public class RegisterModel : PageModel
{
// Disable the registration route. This seems hacky, but it's the way to do it if you're using Identity's default UI
// TODO: Kill this when we'r enot using .DefaultUI() any more.
public IActionResult OnGet()
{
return NotFound();
}
public IActionResult OnPost()
{
return NotFound();
}
}
}
@{
// TODO: Kill this when we no longer need Identity's DefaultUI
Layout = "~/Views/Shared/_Layout.cshtml";
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Monocle.Web.Models;
using System;
using System.Collections.Generic;
......@@ -8,6 +9,7 @@ using System.Threading.Tasks;
namespace Monocle.Web.Controllers.Console
{
[Route("[controller]/{action}")]
[Authorize]
public class HostController : Controller
{
private ApplicationDbContext Database { get; }
......
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Monocle.Web.Models;
using Monocle.Web.Observers;
......@@ -19,6 +20,7 @@ namespace Monocle.Web.Controllers
}
[Route("/")]
[Authorize]
public ActionResult Index()
{
IEnumerable<Host> hosts = Database.Hosts.Include(h => h.Services).ToList();
......
......@@ -9,7 +9,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
</ItemGroup>
......
......@@ -5,6 +5,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
......@@ -13,6 +14,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Pomelo.EntityFrameworkCore.MySql;
using Monocle.Web.Workers;
using Microsoft.AspNetCore.Http;
namespace Monocle.Web
{
......@@ -32,6 +34,19 @@ namespace Monocle.Web
services.AddControllersWithViews();
services.AddHostedService<ObserverWorker>();
//Authentication
services.AddDefaultIdentity<IdentityUser<int>>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI();
services.ConfigureApplicationCookie(options =>
{
options.Cookie.IsEssential = true;
options.Cookie.Name = "MonocleAuthToken";
options.Cookie.MaxAge = TimeSpan.FromDays(30);
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
......@@ -46,6 +61,7 @@ namespace Monocle.Web
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseStaticFiles();
......@@ -53,6 +69,7 @@ namespace Monocle.Web
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
}
......
......@@ -9,9 +9,11 @@
<header>
<h1>Monocle</h1>
</header>
@RenderBody()
@RenderBody()
<footer>
&copy; 2018-@DateTime.UtcNow.Year Brian Parks and contributors
</footer>
@RenderSection("Scripts", required: false)
</body>
</html>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment