From 99e2b2a0cd34dbdcea38220406a0f0c6ae1ab762 Mon Sep 17 00:00:00 2001 From: Dimitar Byalkov Date: Fri, 31 Mar 2023 01:01:20 +0200 Subject: [PATCH] asp.net/winforms login; uml managers, repositories pages edited --- .../HouseData/Managers/UserManager.cs | 22 +++- .../HouseData/Repositories/UserRepository.cs | 1 + .../HouseData/StudentHouseDashboard.csproj | 1 + .../StudentHouseDashboard.sln | 6 + .../WebApp/Pages/Error/401.cshtml | 12 ++ .../WebApp/Pages/Error/401.cshtml.cs | 12 ++ .../WebApp/Pages/Index.cshtml | 7 -- .../WebApp/Pages/Login.cshtml.cs | 31 +++-- .../WebApp/Pages/Shared/_Layout.cshtml | 7 +- StudentHouseDashboard/WebApp/Program.cs | 7 ++ .../WinForms/Login.Designer.cs | 117 ++++++++++++++++++ StudentHouseDashboard/WinForms/Login.cs | 27 ++++ StudentHouseDashboard/WinForms/Login.resx | 60 +++++++++ StudentHouseDashboard/WinForms/Program.cs | 17 +++ .../WinForms/WinForms.csproj | 15 +++ docs/umlclass.vsdx | Bin 68165 -> 79415 bytes 16 files changed, 322 insertions(+), 20 deletions(-) create mode 100644 StudentHouseDashboard/WebApp/Pages/Error/401.cshtml create mode 100644 StudentHouseDashboard/WebApp/Pages/Error/401.cshtml.cs create mode 100644 StudentHouseDashboard/WinForms/Login.Designer.cs create mode 100644 StudentHouseDashboard/WinForms/Login.cs create mode 100644 StudentHouseDashboard/WinForms/Login.resx create mode 100644 StudentHouseDashboard/WinForms/Program.cs create mode 100644 StudentHouseDashboard/WinForms/WinForms.csproj diff --git a/StudentHouseDashboard/HouseData/Managers/UserManager.cs b/StudentHouseDashboard/HouseData/Managers/UserManager.cs index 0a8fa7a..71ea43e 100644 --- a/StudentHouseDashboard/HouseData/Managers/UserManager.cs +++ b/StudentHouseDashboard/HouseData/Managers/UserManager.cs @@ -1,9 +1,12 @@ -using StudentHouseDashboard.Models; +using BCrypt.Net; +using StudentHouseDashboard.Models; using StudentHouseDashboard.Repositories; using System; using System.Collections.Generic; using System.Data; using System.Linq; +using System.Net.Http; +using System.Security.Claims; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; @@ -29,6 +32,23 @@ namespace StudentHouseDashboard.Managers { return userRepository.GetUsersByPage(p, c); } + public User? AuthenticatedUser(string name, string password) + { + List users = userRepository.GetAllUsers(); + User user = users.Find(x => x.Name == name); + if (user == null) + { + return null; + } + else + { + if (BCrypt.Net.BCrypt.Verify(password, user.Password)) + { + return user; + } + else return null; + } + } public bool CreateUser(string name, string password, UserRole role) { return userRepository.CreateUser(name, password, role); diff --git a/StudentHouseDashboard/HouseData/Repositories/UserRepository.cs b/StudentHouseDashboard/HouseData/Repositories/UserRepository.cs index 39cc686..c3168ff 100644 --- a/StudentHouseDashboard/HouseData/Repositories/UserRepository.cs +++ b/StudentHouseDashboard/HouseData/Repositories/UserRepository.cs @@ -154,5 +154,6 @@ namespace StudentHouseDashboard.Repositories else return false; } } + } } diff --git a/StudentHouseDashboard/HouseData/StudentHouseDashboard.csproj b/StudentHouseDashboard/HouseData/StudentHouseDashboard.csproj index 4c947ef..c149945 100644 --- a/StudentHouseDashboard/HouseData/StudentHouseDashboard.csproj +++ b/StudentHouseDashboard/HouseData/StudentHouseDashboard.csproj @@ -7,6 +7,7 @@ + diff --git a/StudentHouseDashboard/StudentHouseDashboard.sln b/StudentHouseDashboard/StudentHouseDashboard.sln index 03de8ce..2aded82 100644 --- a/StudentHouseDashboard/StudentHouseDashboard.sln +++ b/StudentHouseDashboard/StudentHouseDashboard.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp", "WebApp\WebApp.csp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StudentHouseDashboard", "HouseData\StudentHouseDashboard.csproj", "{9A1E1400-9B85-416B-B3B2-2282E0060CE3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinForms", "WinForms\WinForms.csproj", "{3967DDCF-BA8E-45CD-895D-626CE346D419}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {9A1E1400-9B85-416B-B3B2-2282E0060CE3}.Debug|Any CPU.Build.0 = Debug|Any CPU {9A1E1400-9B85-416B-B3B2-2282E0060CE3}.Release|Any CPU.ActiveCfg = Release|Any CPU {9A1E1400-9B85-416B-B3B2-2282E0060CE3}.Release|Any CPU.Build.0 = Release|Any CPU + {3967DDCF-BA8E-45CD-895D-626CE346D419}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3967DDCF-BA8E-45CD-895D-626CE346D419}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3967DDCF-BA8E-45CD-895D-626CE346D419}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3967DDCF-BA8E-45CD-895D-626CE346D419}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/StudentHouseDashboard/WebApp/Pages/Error/401.cshtml b/StudentHouseDashboard/WebApp/Pages/Error/401.cshtml new file mode 100644 index 0000000..1d534d8 --- /dev/null +++ b/StudentHouseDashboard/WebApp/Pages/Error/401.cshtml @@ -0,0 +1,12 @@ +@page +@model WebApp.Pages.Error._401Model +@{ + ViewData["Title"] = "401 Unauthorised"; +} +
+

Error 401 Unauthorised

+ +

+ You do not have rights to access this page! +

+
\ No newline at end of file diff --git a/StudentHouseDashboard/WebApp/Pages/Error/401.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/Error/401.cshtml.cs new file mode 100644 index 0000000..9a4b419 --- /dev/null +++ b/StudentHouseDashboard/WebApp/Pages/Error/401.cshtml.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace WebApp.Pages.Error +{ + public class _401Model : PageModel + { + public void OnGet() + { + } + } +} diff --git a/StudentHouseDashboard/WebApp/Pages/Index.cshtml b/StudentHouseDashboard/WebApp/Pages/Index.cshtml index 0ede26a..5c61320 100644 --- a/StudentHouseDashboard/WebApp/Pages/Index.cshtml +++ b/StudentHouseDashboard/WebApp/Pages/Index.cshtml @@ -15,13 +15,6 @@ -
-

Why choose our solution?

-
-

Student House Dashboard helps tenants organise common household chores even if they don't know each other very well.

-
-
-

Is it difficult to use?

diff --git a/StudentHouseDashboard/WebApp/Pages/Login.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/Login.cshtml.cs index ef4dc1f..3979923 100644 --- a/StudentHouseDashboard/WebApp/Pages/Login.cshtml.cs +++ b/StudentHouseDashboard/WebApp/Pages/Login.cshtml.cs @@ -1,7 +1,10 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; using StudentHouseDashboard.Models; using StudentHouseDashboard.Managers; +using System.Security.Claims; namespace WebApp.Pages { @@ -14,19 +17,33 @@ namespace WebApp.Pages { } - public void OnPost() + public IActionResult OnPost(string? returnUrl) { var userManager = new UserManager(); - - foreach (var item in userManager.GetAllUsers()) + User? user = userManager.AuthenticatedUser(MyUser.Name, MyUser.Password); + if (user != null) { - if (item.Name == MyUser.Name && BCrypt.Net.BCrypt.Verify(MyUser.Password, item.Password)) + List claims = new List(); + claims.Add(new Claim(ClaimTypes.Name, user.Name)); + claims.Add(new Claim("id", user.ID.ToString())); + claims.Add(new Claim(ClaimTypes.Role, user.Role.ToString())); + var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); + HttpContext.SignInAsync(new ClaimsPrincipal(claimsIdentity)); + // ViewData["confirm"] = $"Welcome, {MyUser.Name}! {MyUser.ID}, {MyUser.Password}, {MyUser.Role}"; + if (!String.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl)) { - MyUser = item; - ViewData["confirm"] = $"Welcome, {MyUser.Name}! {MyUser.ID}, {MyUser.Password}, {MyUser.Role}"; + return Redirect(returnUrl); + } + else + { + return RedirectToPage("Announcements"); } } - + else + { + ModelState.AddModelError("InvalidCredentials", "The supplied username and/or password is invalid"); + return Page(); + } } } } diff --git a/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml b/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml index aed1928..74662f4 100644 --- a/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml +++ b/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml @@ -23,10 +23,7 @@ Home -