added authentication, adjusted controller names
This commit is contained in:
parent
7657169e1e
commit
89a8b1a28b
54
Controllers/LoginController.cs
Normal file
54
Controllers/LoginController.cs
Normal 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_PASS_PHRASE");
|
||||||
|
|
||||||
|
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("/");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,15 @@
|
|||||||
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;
|
||||||
}
|
}
|
15
Program.cs
15
Program.cs
@ -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");
|
||||||
|
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development",
|
||||||
|
"APP_PASS_PHRASE": "123456"
|
||||||
},
|
},
|
||||||
"dotnetRunMessages": true,
|
"dotnetRunMessages": true,
|
||||||
"applicationUrl": "http://localhost:5015"
|
"applicationUrl": "http://localhost:5015"
|
||||||
|
9
Views/Login/Login.cshtml
Normal file
9
Views/Login/Login.cshtml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
@{
|
||||||
|
ViewBag.Title = "Login";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Html.ValidationSummary()
|
||||||
|
|
||||||
|
<form method="post">
|
||||||
|
<input type="password" name="passPhrase" placeholder="Pass Phrase" />
|
||||||
|
</form>
|
@ -63,3 +63,13 @@ 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;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user