Permalink
Browse files

Initial Fasetto Word Web Server with user login

  • Loading branch information...
angelsix committed Jan 18, 2018
1 parent 33af63f commit 23f45470a13e34e2be2990e0e9e4c59be7474886
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Fasetto.Word.Web.Server", "Fasetto.Word.Web.Server/Fasetto.Word.Web.Server.csproj", "{B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {75575559-DA47-4D16-803A-735E85EACA3D}
EndGlobalSection
EndGlobal
@@ -0,0 +1,157 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
namespace Fasetto.Word.Web.Server
{
public class HomeController : Controller
{
#region Protected Members
/// <summary>
/// The scoped Application context
/// </summary>
protected ApplicationDbContext mContext;
/// <summary>
/// The manager for handling user creation, deletion, searching, roles etc...
/// </summary>
protected UserManager<ApplicationUser> mUserManager;
/// <summary>
/// The manager for handling signing in and out for our users
/// </summary>
protected SignInManager<ApplicationUser> mSignInManager;
#endregion
#region Constructor
/// <summary>
/// Default constructor
/// </summary>
/// <param name="context">The injected context</param>
public HomeController(
ApplicationDbContext context,
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager)
{
mContext = context;
mUserManager = userManager;
mSignInManager = signInManager;
}
#endregion
/// <summary>
/// Basic welcome page
/// </summary>
/// <returns></returns>
public IActionResult Index()
{
// Make sure we have the database
mContext.Database.EnsureCreated();
// If we have no settings already...
if (!mContext.Settings.Any())
{
// Add a new setting
mContext.Settings.Add(new SettingsDataModel
{
Name = "BackgroundColor",
Value = "Red"
});
// Check to show the new setting is currently only local and not in the database
var settingsLocally = mContext.Settings.Local.Count();
var settingsDatabase = mContext.Settings.Count();
var firstLocal = mContext.Settings.Local.FirstOrDefault();
var firstDatabase = mContext.Settings.FirstOrDefault();
// Commit setting to database
mContext.SaveChanges();
// Recheck to show its now in local and the actual database
settingsLocally = mContext.Settings.Local.Count();
settingsDatabase = mContext.Settings.Count();
firstLocal = mContext.Settings.Local.FirstOrDefault();
firstDatabase = mContext.Settings.FirstOrDefault();
}
return View();
}
/// <summary>
/// Creates our single user for now
/// </summary>
/// <returns></returns>
[Route("create")]
public async Task<IActionResult> CreateUserAsync()
{
var result = await mUserManager.CreateAsync(new ApplicationUser
{
UserName = "angelsix",
Email = "contact@angelsix.com"
}, "password");
if (result.Succeeded)
return Content("User was created", "text/html");
return Content("User creation failed", "text/html");
}
/// <summary>
/// Private area. No peeking
/// </summary>
/// <returns></returns>
[Authorize]
[Route("private")]
public IActionResult Private()
{
return Content($"This is a private area. Welcome {HttpContext.User.Identity.Name}", "text/html");
}
/// <summary>
/// Log the user out
/// </summary>
/// <returns></returns>
[Route("logout")]
public async Task<IActionResult> SignOutAsync()
{
await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme);
return Content("done");
}
/// <summary>
/// An auto-login page for testing
/// </summary>
/// <param name="returnUrl">The url to return to if successfully logged in</param>
/// <returns></returns>
[Route("login")]
public async Task<IActionResult> LoginAsync(string returnUrl)
{
// Sign out any previous sessions
await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme);
// Sign user in with the valid credentials
var result = await mSignInManager.PasswordSignInAsync("angelsix", "password", true, false);
// If successful...
if (result.Succeeded)
{
// If we have no return URL...
if (string.IsNullOrEmpty(returnUrl))
// Go to home
return RedirectToAction(nameof(Index));
// Otherwise, go to the return url
return Redirect(returnUrl);
}
return Content("Failed to login", "text/html");
}
}
}
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace Fasetto.Word.Web.Server
{
/// <summary>
/// The database representational model for our application
/// </summary>
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
#region Public Properties
/// <summary>
/// The settings for the application
/// </summary>
public DbSet<SettingsDataModel> Settings { get; set; }
#endregion
#region Constructor
/// <summary>
/// Default constructor, expecting database options passed in
/// </summary>
/// <param name="options">The database context options</param>
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
#endregion
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Fluent API
modelBuilder.Entity<SettingsDataModel>().HasIndex(a => a.Name);
}
}
}
@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Identity;
namespace Fasetto.Word.Web.Server
{
/// <summary>
/// The user data and profile for our application
/// </summary>
public class ApplicationUser : IdentityUser
{
}
}
@@ -0,0 +1,31 @@
using System.ComponentModel.DataAnnotations;
namespace Fasetto.Word.Web.Server
{
/// <summary>
/// Our Settings database table representational model
/// </summary>
public class SettingsDataModel
{
/// <summary>
/// The unique Id for this entry
/// </summary>
[Key]
public string Id { get; set; }
/// <summary>
/// The settings name
/// </summary>
/// <remarks>This column is indexed</remarks>
[Required]
[MaxLength(256)]
public string Name { get; set; }
/// <summary>
/// The settings value
/// </summary>
[Required]
[MaxLength(2048)]
public string Value { get; set; }
}
}
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>
<ItemGroup>
<Content Remove="wwwroot\style.xaml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
</Project>
@@ -0,0 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
namespace Fasetto.Word.Web.Server
{
/// <summary>
/// A shorthand access class to get DI services with nice clean short code
/// </summary>
public static class IoC
{
/// <summary>
/// The scoped instance of the <see cref="ApplicationDbContext"/>
/// </summary>
public static ApplicationDbContext ApplicationDbContext => IoCContainer.Provider.GetService<ApplicationDbContext>();
}
/// <summary>
/// The dependency injection container making use of the built in .Net Core service provider
/// </summary>
public static class IoCContainer
{
/// <summary>
/// The service provider for this application
/// </summary>
public static ServiceProvider Provider { get; set; }
}
}
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace Fasetto.Word.Web.Server
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder()
.UseStartup<Startup>()
.Build();
}
}
}
@@ -0,0 +1,12 @@
{
"profiles": {
"kestrel": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000/"
}
}
}
Oops, something went wrong.

0 comments on commit 23f4547

Please sign in to comment.