Merge branch 'master' into dev/multiple-notes

This commit is contained in:
Ryan Peters 2023-05-17 21:22:48 -04:00
commit 8e7b9c9622
9 changed files with 122 additions and 9 deletions

View File

@ -0,0 +1,54 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
namespace BinaryDad.Notes.Controllers
{
public class LoginController : Controller
{
[Route("login")]
public IActionResult Login()
{
return View();
}
[ActionName(nameof(Login))]
[Route("login")]
[HttpPost]
public async Task<IActionResult> LoginPost([Required] string passphrase, string returnUrl)
{
if (ModelState.IsValid)
{
var appPassphrase = Environment.GetEnvironmentVariable("APP_PASSPHRASE");
if (passphrase == appPassphrase)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.GivenName, "Ryan")
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignInAsync(new ClaimsPrincipal(claimsIdentity));
return Redirect(returnUrl);
}
ModelState.AddModelError("", "Invalid login");
}
return View();
}
[Route("logout")]
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync();
return Redirect("/");
}
}
}

View File

@ -1,14 +1,16 @@
using BinaryDad.Notes.Models; using BinaryDad.Notes.Models;
using BinaryDad.Notes.Services; using BinaryDad.Notes.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace BinaryDad.Notes.Controllers; namespace BinaryDad.Notes.Controllers;
public class HomeController : Controller [Authorize]
public class NoteController : Controller
{ {
private readonly INoteService noteService; private readonly INoteService noteService;
public HomeController(INoteService noteService) public NoteController(INoteService noteService)
{ {
this.noteService = noteService; this.noteService = noteService;
} }

View File

@ -1,5 +1,6 @@
using BinaryDad.Notes; using BinaryDad.Notes;
using BinaryDad.Notes.Services; using BinaryDad.Notes.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -8,6 +9,16 @@ builder.Services.AddControllersWithViews();
builder.Services.AddSignalR(); builder.Services.AddSignalR();
builder.Services.AddSingleton<INoteService, FileNoteService>(); builder.Services.AddSingleton<INoteService, FileNoteService>();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
});
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(o =>
{
o.LoginPath = "/login";
});
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
@ -19,12 +30,12 @@ if (!app.Environment.IsDevelopment())
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.MapControllerRoute( app.MapControllerRoute(
name: "default", name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"); pattern: "{controller=Note}/{action=Index}/{id?}");
app.MapHub<NoteHub>("/noteHub"); app.MapHub<NoteHub>("/noteHub");

View File

@ -4,7 +4,8 @@
"commandName": "Project", "commandName": "Project",
"launchBrowser": true, "launchBrowser": true,
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development",
"APP_PASSPHRASE": "123456"
}, },
"dotnetRunMessages": true, "dotnetRunMessages": true,
"applicationUrl": "http://localhost:5015" "applicationUrl": "http://localhost:5015"

18
Views/Login/Login.cshtml Normal file
View File

@ -0,0 +1,18 @@
@{
ViewBag.Title = "Login";
}
@Html.ValidationSummary()
<form method="post">
<input type="password" name="passphrase" placeholder="Passphrase" />
</form>
@section scripts {
<script>
$(function () {
$('input[name=passphrase]').focus();
});
</script>
}

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, user-scalable=0;" /> <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, user-scalable=no, user-scalable=0" />
<title>Notes</title> <title>Notes</title>
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" /> <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
</head> </head>
@ -14,6 +14,8 @@
<script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/signalr/dist/browser/signalr.min.js"></script> <script src="~/lib/signalr/dist/browser/signalr.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script> <script src="~/js/site.js" asp-append-version="true"></script>
@RenderSection("scripts", false)
</body> </body>
</html> </html>

View File

@ -79,4 +79,14 @@ textarea {
.toast#update-indicator { .toast#update-indicator {
background-color: orangered; background-color: orangered;
} }
form input[type=password] {
display: block;
margin: 20px auto;
font-size: 20px;
padding: 8px;
border: 1px solid #999;
border-radius: 4px;
color: #999;
}

File diff suppressed because one or more lines are too long