web requires auth by default, renamed context to standard, added application user

This commit is contained in:
andy 2021-10-24 20:15:09 +01:00
parent af980b130a
commit bbfe663797
12 changed files with 57 additions and 28 deletions

View File

@ -47,7 +47,7 @@ namespace Selector.CLI
if (config.DatabaseOptions.Enabled) if (config.DatabaseOptions.Enabled)
{ {
Console.WriteLine("> Adding Databse Context..."); Console.WriteLine("> Adding Databse Context...");
services.AddDbContext<SelectorContext>(options => services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(config.DatabaseOptions.ConnectionString) options.UseNpgsql(config.DatabaseOptions.ConnectionString)
); );
} }

View File

@ -10,11 +10,11 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
namespace Selector.Model namespace Selector.Model
{ {
public class SelectorContext : IdentityDbContext public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{ {
public DbSet<Watcher> Watcher { get; set; } public DbSet<Watcher> Watcher { get; set; }
public SelectorContext(DbContextOptions<SelectorContext> options) : base(options) public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{ {
} }
@ -30,19 +30,19 @@ namespace Selector.Model
} }
} }
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<SelectorContext> public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{ {
public SelectorContext CreateDbContext(string[] args) public ApplicationDbContext CreateDbContext(string[] args)
{ {
IConfigurationRoot configuration = new ConfigurationBuilder() IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) .SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(@Directory.GetCurrentDirectory() + "/../Selector.Web/appsettings.Development.json") .AddJsonFile(@Directory.GetCurrentDirectory() + "/../Selector.Web/appsettings.Development.json")
.Build(); .Build();
var builder = new DbContextOptionsBuilder<SelectorContext>(); var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseNpgsql(configuration.GetConnectionString("Default")); builder.UseNpgsql(configuration.GetConnectionString("Default"));
return new SelectorContext(builder.Options); return new ApplicationDbContext(builder.Options);
} }
} }
} }

View File

@ -0,0 +1,9 @@
using System;
using Microsoft.AspNetCore.Identity;
namespace Selector.Model
{
public class ApplicationUser : IdentityUser
{
}
}

View File

@ -15,7 +15,7 @@ namespace Selector.Web.Areas.Identity
public void Configure(IWebHostBuilder builder) public void Configure(IWebHostBuilder builder)
{ {
builder.ConfigureServices((context, services) => { builder.ConfigureServices((context, services) => {
//services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true); //services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true);
}); });
} }
} }

View File

@ -12,18 +12,20 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Selector.Model;
namespace Selector.Web.Areas.Identity.Pages.Account namespace Selector.Web.Areas.Identity.Pages.Account
{ {
[AllowAnonymous] [AllowAnonymous]
public class LoginModel : PageModel public class LoginModel : PageModel
{ {
private readonly UserManager<IdentityUser> _userManager; private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<IdentityUser> _signInManager; private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger<LoginModel> _logger; private readonly ILogger<LoginModel> _logger;
public LoginModel(SignInManager<IdentityUser> signInManager, public LoginModel(SignInManager<ApplicationUser> signInManager,
ILogger<LoginModel> logger, ILogger<LoginModel> logger,
UserManager<IdentityUser> userManager) UserManager<ApplicationUser> userManager)
{ {
_userManager = userManager; _userManager = userManager;
_signInManager = signInManager; _signInManager = signInManager;

View File

@ -8,15 +8,17 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Selector.Model;
namespace Selector.Web.Areas.Identity.Pages.Account namespace Selector.Web.Areas.Identity.Pages.Account
{ {
[AllowAnonymous] [AllowAnonymous]
public class LogoutModel : PageModel public class LogoutModel : PageModel
{ {
private readonly SignInManager<IdentityUser> _signInManager; private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger<LogoutModel> _logger; private readonly ILogger<LogoutModel> _logger;
public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger) public LogoutModel(SignInManager<ApplicationUser> signInManager, ILogger<LogoutModel> logger)
{ {
_signInManager = signInManager; _signInManager = signInManager;
_logger = logger; _logger = logger;

View File

@ -14,19 +14,21 @@ using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities; using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Selector.Model;
namespace Selector.Web.Areas.Identity.Pages.Account namespace Selector.Web.Areas.Identity.Pages.Account
{ {
[AllowAnonymous] [AllowAnonymous]
public class RegisterModel : PageModel public class RegisterModel : PageModel
{ {
private readonly SignInManager<IdentityUser> _signInManager; private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<IdentityUser> _userManager; private readonly UserManager<ApplicationUser> _userManager;
private readonly ILogger<RegisterModel> _logger; private readonly ILogger<RegisterModel> _logger;
private readonly IEmailSender _emailSender; private readonly IEmailSender _emailSender;
public RegisterModel( public RegisterModel(
UserManager<IdentityUser> userManager, UserManager<ApplicationUser> userManager,
SignInManager<IdentityUser> signInManager, SignInManager<ApplicationUser> signInManager,
ILogger<RegisterModel> logger, ILogger<RegisterModel> logger,
IEmailSender emailSender) IEmailSender emailSender)
{ {
@ -74,7 +76,7 @@ namespace Selector.Web.Areas.Identity.Pages.Account
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
if (ModelState.IsValid) 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); var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded) if (result.Succeeded)
{ {

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Selector.Model; using Selector.Model;
@ -12,18 +13,18 @@ namespace Selector.Web.Controller {
[Route("api/[controller]")] [Route("api/[controller]")]
public class TestController { public class TestController {
private readonly SelectorContext db; private readonly ApplicationDbContext db;
public TestController(SelectorContext context) public TestController(ApplicationDbContext context)
{ {
db = context; db = context;
} }
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<Watcher>>> Get() public async Task<ActionResult<IEnumerable<ApplicationUser>>> Get()
{ {
// var watchers = ; // var watchers = ;
return await db.Watcher.ToListAsync(); return await db.Users.ToListAsync();
} }
} }
} }

View File

@ -4,10 +4,12 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Selector.Web.Pages namespace Selector.Web.Pages
{ {
[AllowAnonymous]
public class IndexModel : PageModel public class IndexModel : PageModel
{ {
private readonly ILogger<IndexModel> _logger; private readonly ILogger<IndexModel> _logger;

View File

@ -4,10 +4,12 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Selector.Web.Pages namespace Selector.Web.Pages
{ {
[AllowAnonymous]
public class PrivacyModel : PageModel public class PrivacyModel : PageModel
{ {
private readonly ILogger<PrivacyModel> _logger; private readonly ILogger<PrivacyModel> _logger;

View File

@ -1,7 +1,8 @@
@using Microsoft.AspNetCore.Identity @using Microsoft.AspNetCore.Identity
@using Selector.Model;
@inject SignInManager<IdentityUser> SignInManager @inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<IdentityUser> UserManager @inject UserManager<ApplicationUser> UserManager
<ul class="navbar-nav"> <ul class="navbar-nav">
@if (SignInManager.IsSignedIn(User)) @if (SignInManager.IsSignedIn(User))

View File

@ -10,6 +10,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Selector.Model; using Selector.Model;
@ -31,12 +32,12 @@ namespace Selector.Web
services.AddRazorPages().AddRazorRuntimeCompilation(); services.AddRazorPages().AddRazorRuntimeCompilation();
services.AddControllers(); services.AddControllers();
services.AddDbContext<SelectorContext>(options => services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("Default")) options.UseNpgsql(Configuration.GetConnectionString("Default"))
); );
services.AddIdentity<IdentityUser, IdentityRole>() services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<SelectorContext>() .AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI() .AddDefaultUI()
.AddDefaultTokenProviders(); .AddDefaultTokenProviders();
@ -72,6 +73,13 @@ namespace Selector.Web
options.AccessDeniedPath = "/Identity/Account/AccessDenied"; options.AccessDeniedPath = "/Identity/Account/AccessDenied";
options.SlidingExpiration = true; options.SlidingExpiration = true;
}); });
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.