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)
{
Console.WriteLine("> Adding Databse Context...");
services.AddDbContext<SelectorContext>(options =>
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(config.DatabaseOptions.ConnectionString)
);
}

View File

@ -10,11 +10,11 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
namespace Selector.Model
{
public class SelectorContext : IdentityDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
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()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(@Directory.GetCurrentDirectory() + "/../Selector.Web/appsettings.Development.json")
.Build();
var builder = new DbContextOptionsBuilder<SelectorContext>();
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
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)
{
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.Extensions.Logging;
using Selector.Model;
namespace Selector.Web.Areas.Identity.Pages.Account
{
[AllowAnonymous]
public class LoginModel : PageModel
{
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger<LoginModel> _logger;
public LoginModel(SignInManager<IdentityUser> signInManager,
public LoginModel(SignInManager<ApplicationUser> signInManager,
ILogger<LoginModel> logger,
UserManager<IdentityUser> userManager)
UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
_signInManager = signInManager;

View File

@ -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<IdentityUser> _signInManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger<LogoutModel> _logger;
public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger)
public LogoutModel(SignInManager<ApplicationUser> signInManager, ILogger<LogoutModel> logger)
{
_signInManager = signInManager;
_logger = logger;

View File

@ -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<IdentityUser> _signInManager;
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager;
private readonly ILogger<RegisterModel> _logger;
private readonly IEmailSender _emailSender;
public RegisterModel(
UserManager<IdentityUser> userManager,
SignInManager<IdentityUser> signInManager,
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
ILogger<RegisterModel> 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)
{

View File

@ -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<ActionResult<IEnumerable<Watcher>>> Get()
public async Task<ActionResult<IEnumerable<ApplicationUser>>> Get()
{
// 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 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<IndexModel> _logger;

View File

@ -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<PrivacyModel> _logger;

View File

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

View File

@ -10,6 +10,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Selector.Model;
@ -31,12 +32,12 @@ namespace Selector.Web
services.AddRazorPages().AddRazorRuntimeCompilation();
services.AddControllers();
services.AddDbContext<SelectorContext>(options =>
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("Default"))
);
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<SelectorContext>()
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
@ -72,6 +73,13 @@ namespace Selector.Web
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
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.