asp.net/winforms login; uml managers, repositories pages edited

This commit is contained in:
Dimitar Byalkov
2023-03-31 01:01:20 +02:00
parent 78eba13712
commit 99e2b2a0cd
16 changed files with 322 additions and 20 deletions

View File

@@ -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<User> 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);

View File

@@ -154,5 +154,6 @@ namespace StudentHouseDashboard.Repositories
else return false;
}
}
}
}

View File

@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
</ItemGroup>

View File

@@ -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

View File

@@ -0,0 +1,12 @@
@page
@model WebApp.Pages.Error._401Model
@{
ViewData["Title"] = "401 Unauthorised";
}
<div class="text-center">
<h1 class="display-4">Error 401 Unauthorised</h1>
<img src="http://http.cat/401" />
<p>
You do not have rights to access this page!
</p>
</div>

View File

@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebApp.Pages.Error
{
public class _401Model : PageModel
{
public void OnGet()
{
}
}
}

View File

@@ -15,13 +15,6 @@
</div>
</div>
<div class="px-4 py-5 my-5 text-center">
<h2 class="display-5 fw-bold">Why choose our solution?</h2>
<div class="col-lg-6 mx-auto">
<p class="lead mb-4">Student House Dashboard helps tenants organise common household chores even if they don't know each other very well.</p>
</div>
</div>
<div class="px-4 py-5 my-5 text-center">
<h2 class="display-5 fw-bold">Is it difficult to use?</h2>
<div class="col-lg-6 mx-auto">

View File

@@ -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<Claim> claims = new List<Claim>();
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();
}
}
}
}

View File

@@ -23,10 +23,7 @@
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Contact">Contact</a>
<a class="nav-link text-dark" asp-area="" asp-page="/Announcements">Announcements</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Login">Login</a>
@@ -44,7 +41,7 @@
<footer class="border-top footer text-muted">
<div class="container">
&copy; 2023 - StudentHouseDashboard - <a asp-area="" asp-page="/Privacy">Privacy</a>
<p>StudentHouseDashboard &copy; 2023 <a asp-area="" asp-page="/Privacy">Privacy</a> <a asp-area="" asp-page="/Contact">Contact</a></p>
</div>
</footer>

View File

@@ -1,3 +1,5 @@
using Microsoft.AspNetCore.Authentication.Cookies;
namespace WebApp
{
public class Program
@@ -9,6 +11,11 @@ namespace WebApp
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => {
options.LoginPath = new PathString("/Login");
options.AccessDeniedPath = new PathString("/Error/401");
});
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@@ -0,0 +1,117 @@
namespace WinForms
{
partial class Login
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
lblLogo = new Label();
lblUsername = new Label();
tbUsername = new TextBox();
lblPassword = new Label();
tbPassword = new TextBox();
btnLogin = new Button();
SuspendLayout();
//
// lblLogo
//
lblLogo.Location = new Point(12, 9);
lblLogo.Name = "lblLogo";
lblLogo.Size = new Size(230, 37);
lblLogo.TabIndex = 0;
lblLogo.Text = "Welcome to Student House Dashboard";
lblLogo.TextAlign = ContentAlignment.MiddleCenter;
//
// lblUsername
//
lblUsername.AutoSize = true;
lblUsername.Location = new Point(12, 64);
lblUsername.Name = "lblUsername";
lblUsername.Size = new Size(63, 15);
lblUsername.TabIndex = 1;
lblUsername.Text = "Username:";
//
// tbUsername
//
tbUsername.Location = new Point(81, 61);
tbUsername.Name = "tbUsername";
tbUsername.Size = new Size(161, 23);
tbUsername.TabIndex = 2;
//
// lblPassword
//
lblPassword.AutoSize = true;
lblPassword.Location = new Point(12, 93);
lblPassword.Name = "lblPassword";
lblPassword.Size = new Size(60, 15);
lblPassword.TabIndex = 3;
lblPassword.Text = "Password:";
//
// tbPassword
//
tbPassword.Location = new Point(81, 90);
tbPassword.Name = "tbPassword";
tbPassword.Size = new Size(161, 23);
tbPassword.TabIndex = 4;
//
// btnLogin
//
btnLogin.Location = new Point(167, 119);
btnLogin.Name = "btnLogin";
btnLogin.Size = new Size(75, 23);
btnLogin.TabIndex = 5;
btnLogin.Text = "Login";
btnLogin.UseVisualStyleBackColor = true;
btnLogin.Click += btnLogin_Click;
//
// Login
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(266, 163);
Controls.Add(btnLogin);
Controls.Add(tbPassword);
Controls.Add(lblPassword);
Controls.Add(tbUsername);
Controls.Add(lblUsername);
Controls.Add(lblLogo);
Name = "Login";
ShowIcon = false;
Text = "StudentHouseDashboard";
ResumeLayout(false);
PerformLayout();
}
#endregion
private Label lblLogo;
private Label lblUsername;
private TextBox tbUsername;
private Label lblPassword;
private TextBox tbPassword;
private Button btnLogin;
}
}

View File

@@ -0,0 +1,27 @@
using StudentHouseDashboard.Managers;
using StudentHouseDashboard.Models;
namespace WinForms
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
UserManager userManager = new UserManager();
User? user = userManager.AuthenticatedUser(tbUsername.Text, tbPassword.Text);
if (user == null)
{
MessageBox.Show("Wrong username or password", "Login failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show($"Welcome, {user.Name}", "Login successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}

View File

@@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@@ -0,0 +1,17 @@
namespace WinForms
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Login());
}
}
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\HouseData\StudentHouseDashboard.csproj" />
</ItemGroup>
</Project>