From bbfe66379770c99503c327b951524554557f0ab8 Mon Sep 17 00:00:00 2001 From: andy Date: Sun, 24 Oct 2021 20:15:09 +0100 Subject: [PATCH] web requires auth by default, renamed context to standard, added application user --- Selector.CLI/Program.cs | 2 +- ...{SelectorContext.cs => ApplicationDbContext.cs} | 12 ++++++------ Selector.Model/ApplicationUser.cs | 9 +++++++++ .../Areas/Identity/IdentityHostingStartup.cs | 2 +- .../Areas/Identity/Pages/Account/Login.cshtml.cs | 10 ++++++---- .../Areas/Identity/Pages/Account/Logout.cshtml.cs | 6 ++++-- .../Identity/Pages/Account/Register.cshtml.cs | 12 +++++++----- Selector.Web/Controller/TestController.cs | 9 +++++---- Selector.Web/Pages/Index.cshtml.cs | 2 ++ Selector.Web/Pages/Privacy.cshtml.cs | 2 ++ Selector.Web/Pages/Shared/_LoginPartial.cshtml | 5 +++-- Selector.Web/Startup.cs | 14 +++++++++++--- 12 files changed, 57 insertions(+), 28 deletions(-) rename Selector.Model/{SelectorContext.cs => ApplicationDbContext.cs} (71%) create mode 100644 Selector.Model/ApplicationUser.cs diff --git a/Selector.CLI/Program.cs b/Selector.CLI/Program.cs index aaec12a..2529392 100644 --- a/Selector.CLI/Program.cs +++ b/Selector.CLI/Program.cs @@ -47,7 +47,7 @@ namespace Selector.CLI if (config.DatabaseOptions.Enabled) { Console.WriteLine("> Adding Databse Context..."); - services.AddDbContext(options => + services.AddDbContext(options => options.UseNpgsql(config.DatabaseOptions.ConnectionString) ); } diff --git a/Selector.Model/SelectorContext.cs b/Selector.Model/ApplicationDbContext.cs similarity index 71% rename from Selector.Model/SelectorContext.cs rename to Selector.Model/ApplicationDbContext.cs index db82cbc..ffde2e2 100644 --- a/Selector.Model/SelectorContext.cs +++ b/Selector.Model/ApplicationDbContext.cs @@ -10,11 +10,11 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore; namespace Selector.Model { - public class SelectorContext : IdentityDbContext + public class ApplicationDbContext : IdentityDbContext { public DbSet Watcher { get; set; } - public SelectorContext(DbContextOptions options) : base(options) + public ApplicationDbContext(DbContextOptions options) : base(options) { } @@ -30,19 +30,19 @@ namespace Selector.Model } } - public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory + public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory { - public SelectorContext CreateDbContext(string[] args) + public ApplicationDbContext CreateDbContext(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(@Directory.GetCurrentDirectory() + "/../Selector.Web/appsettings.Development.json") .Build(); - var builder = new DbContextOptionsBuilder(); + var builder = new DbContextOptionsBuilder(); builder.UseNpgsql(configuration.GetConnectionString("Default")); - return new SelectorContext(builder.Options); + return new ApplicationDbContext(builder.Options); } } } \ No newline at end of file diff --git a/Selector.Model/ApplicationUser.cs b/Selector.Model/ApplicationUser.cs new file mode 100644 index 0000000..503b851 --- /dev/null +++ b/Selector.Model/ApplicationUser.cs @@ -0,0 +1,9 @@ +using System; +using Microsoft.AspNetCore.Identity; + +namespace Selector.Model +{ + public class ApplicationUser : IdentityUser + { + } +} \ No newline at end of file diff --git a/Selector.Web/Areas/Identity/IdentityHostingStartup.cs b/Selector.Web/Areas/Identity/IdentityHostingStartup.cs index 1ae64a0..8d5f9dc 100644 --- a/Selector.Web/Areas/Identity/IdentityHostingStartup.cs +++ b/Selector.Web/Areas/Identity/IdentityHostingStartup.cs @@ -15,7 +15,7 @@ namespace Selector.Web.Areas.Identity public void Configure(IWebHostBuilder builder) { builder.ConfigureServices((context, services) => { - //services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true); + //services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true); }); } } diff --git a/Selector.Web/Areas/Identity/Pages/Account/Login.cshtml.cs b/Selector.Web/Areas/Identity/Pages/Account/Login.cshtml.cs index b10fcde..d6e76d5 100644 --- a/Selector.Web/Areas/Identity/Pages/Account/Login.cshtml.cs +++ b/Selector.Web/Areas/Identity/Pages/Account/Login.cshtml.cs @@ -12,18 +12,20 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; +using Selector.Model; + namespace Selector.Web.Areas.Identity.Pages.Account { [AllowAnonymous] public class LoginModel : PageModel { - private readonly UserManager _userManager; - private readonly SignInManager _signInManager; + private readonly UserManager _userManager; + private readonly SignInManager _signInManager; private readonly ILogger _logger; - public LoginModel(SignInManager signInManager, + public LoginModel(SignInManager signInManager, ILogger logger, - UserManager userManager) + UserManager userManager) { _userManager = userManager; _signInManager = signInManager; diff --git a/Selector.Web/Areas/Identity/Pages/Account/Logout.cshtml.cs b/Selector.Web/Areas/Identity/Pages/Account/Logout.cshtml.cs index 05c41c6..5cc8bd7 100644 --- a/Selector.Web/Areas/Identity/Pages/Account/Logout.cshtml.cs +++ b/Selector.Web/Areas/Identity/Pages/Account/Logout.cshtml.cs @@ -8,15 +8,17 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; +using Selector.Model; + namespace Selector.Web.Areas.Identity.Pages.Account { [AllowAnonymous] public class LogoutModel : PageModel { - private readonly SignInManager _signInManager; + private readonly SignInManager _signInManager; private readonly ILogger _logger; - public LogoutModel(SignInManager signInManager, ILogger logger) + public LogoutModel(SignInManager signInManager, ILogger logger) { _signInManager = signInManager; _logger = logger; diff --git a/Selector.Web/Areas/Identity/Pages/Account/Register.cshtml.cs b/Selector.Web/Areas/Identity/Pages/Account/Register.cshtml.cs index 524f6dc..1fee536 100644 --- a/Selector.Web/Areas/Identity/Pages/Account/Register.cshtml.cs +++ b/Selector.Web/Areas/Identity/Pages/Account/Register.cshtml.cs @@ -14,19 +14,21 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Logging; +using Selector.Model; + namespace Selector.Web.Areas.Identity.Pages.Account { [AllowAnonymous] public class RegisterModel : PageModel { - private readonly SignInManager _signInManager; - private readonly UserManager _userManager; + private readonly SignInManager _signInManager; + private readonly UserManager _userManager; private readonly ILogger _logger; private readonly IEmailSender _emailSender; public RegisterModel( - UserManager userManager, - SignInManager signInManager, + UserManager userManager, + SignInManager signInManager, ILogger logger, IEmailSender emailSender) { @@ -74,7 +76,7 @@ namespace Selector.Web.Areas.Identity.Pages.Account ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { - var user = new IdentityUser { UserName = Input.Email, Email = Input.Email }; + var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { diff --git a/Selector.Web/Controller/TestController.cs b/Selector.Web/Controller/TestController.cs index fa9e6a0..0621e25 100644 --- a/Selector.Web/Controller/TestController.cs +++ b/Selector.Web/Controller/TestController.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Selector.Model; @@ -12,18 +13,18 @@ namespace Selector.Web.Controller { [Route("api/[controller]")] public class TestController { - private readonly SelectorContext db; + private readonly ApplicationDbContext db; - public TestController(SelectorContext context) + public TestController(ApplicationDbContext context) { db = context; } [HttpGet] - public async Task>> Get() + public async Task>> Get() { // var watchers = ; - return await db.Watcher.ToListAsync(); + return await db.Users.ToListAsync(); } } } \ No newline at end of file diff --git a/Selector.Web/Pages/Index.cshtml.cs b/Selector.Web/Pages/Index.cshtml.cs index 7d94d22..d6da548 100644 --- a/Selector.Web/Pages/Index.cshtml.cs +++ b/Selector.Web/Pages/Index.cshtml.cs @@ -4,10 +4,12 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Logging; namespace Selector.Web.Pages { + [AllowAnonymous] public class IndexModel : PageModel { private readonly ILogger _logger; diff --git a/Selector.Web/Pages/Privacy.cshtml.cs b/Selector.Web/Pages/Privacy.cshtml.cs index a3b8d91..2e987ca 100644 --- a/Selector.Web/Pages/Privacy.cshtml.cs +++ b/Selector.Web/Pages/Privacy.cshtml.cs @@ -4,10 +4,12 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Logging; namespace Selector.Web.Pages { + [AllowAnonymous] public class PrivacyModel : PageModel { private readonly ILogger _logger; diff --git a/Selector.Web/Pages/Shared/_LoginPartial.cshtml b/Selector.Web/Pages/Shared/_LoginPartial.cshtml index 9f617b3..3c4193c 100644 --- a/Selector.Web/Pages/Shared/_LoginPartial.cshtml +++ b/Selector.Web/Pages/Shared/_LoginPartial.cshtml @@ -1,7 +1,8 @@ @using Microsoft.AspNetCore.Identity +@using Selector.Model; -@inject SignInManager SignInManager -@inject UserManager UserManager +@inject SignInManager SignInManager +@inject UserManager UserManager