From 92427c2bed421ecf2bc2b7d15ad03707a037d8f4 Mon Sep 17 00:00:00 2001 From: lukejelse04 Date: Mon, 20 Sep 2021 10:41:19 +0100 Subject: [PATCH 01/15] Created Login Page and update bootstrap files to allow for dark theme --- .vscode/launch.json | 35 + .vscode/tasks.json | 42 + Views/Home/Index.cshtml | 43 +- Views/Shared/_Layout.cshtml | 14 +- wwwroot/images/MainImage.png | Bin 0 -> 2821675 bytes .../bootstrap/dist/css/bootstrap-default.css | 10038 ++++++++++++++++ .../dist/css/bootstrap-default.min.css | 7 + wwwroot/lib/bootstrap/dist/css/bootstrap.css | 2336 ++-- .../lib/bootstrap/dist/css/bootstrap.min.css | 17 +- 9 files changed, 11583 insertions(+), 949 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 wwwroot/images/MainImage.png create mode 100644 wwwroot/lib/bootstrap/dist/css/bootstrap-default.css create mode 100644 wwwroot/lib/bootstrap/dist/css/bootstrap-default.min.css diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..0639eee --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,35 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/net5.0/EFB.dll", + "args": [], + "cwd": "${workspaceFolder}", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..a362bdd --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/EFB.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/EFB.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/EFB.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index d2d19bd..38a226b 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -1,8 +1,43 @@ @{ - ViewData["Title"] = "Home Page"; + ViewData["Title"] = "Welcome"; } -
-

Welcome

-

Learn about building Web apps with ASP.NET Core.

+
+ +
+
+

AutoRouter Login

+ +
+
+ +
+
+ +
+
+ +
+ + + @{ + if (TempData["Error"] != null) + {//If an error has been flagged, information will be displayed to the user + +
+
+ +
+ Warning! @TempData["Error"]
+ } + } +
+
+
+ +
+ +
+
diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index f41e91f..dcbba73 100644 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -9,9 +9,9 @@
-
-
- +
+
From b30ff5ad1445aa9e3bd4c0a78ccbd418e984aeda Mon Sep 17 00:00:00 2001 From: lukejelse04 Date: Sun, 3 Oct 2021 22:35:59 +0100 Subject: [PATCH 03/15] Added Form Authenticator Includes the methods needed to verify the user inputs and to validate that they are correct. --- .../FormAuthenticator/FormAuthenticator.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Controllers/FormAuthenticator/FormAuthenticator.cs diff --git a/Controllers/FormAuthenticator/FormAuthenticator.cs b/Controllers/FormAuthenticator/FormAuthenticator.cs new file mode 100644 index 0000000..b7bef3c --- /dev/null +++ b/Controllers/FormAuthenticator/FormAuthenticator.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EFB.Controllers.FormAuthenticator +{ + public static class FormAuthenticator + { + + public static bool ValidateEMail(string EMail){ + if (EMail.Contains("@") && EMail.Contains(".") && !EMail.Contains(" ")) + { + if (EMail.Count(x => x == '@') == 1) + { + return true; + } + } + return false; + } + + + } +} \ No newline at end of file From e1875fde75fdda60ba18fb7d12a47cf2d2bda089 Mon Sep 17 00:00:00 2001 From: lukejelse04 Date: Sun, 3 Oct 2021 22:37:17 +0100 Subject: [PATCH 04/15] Update FormAuthenticator.cs --- .../FormAuthenticator/FormAuthenticator.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Controllers/FormAuthenticator/FormAuthenticator.cs b/Controllers/FormAuthenticator/FormAuthenticator.cs index b7bef3c..7038e8e 100644 --- a/Controllers/FormAuthenticator/FormAuthenticator.cs +++ b/Controllers/FormAuthenticator/FormAuthenticator.cs @@ -19,6 +19,32 @@ namespace EFB.Controllers.FormAuthenticator return false; } + public static bool ValidateEndpoint(string Endpoint){ + //If it contains http & :// it can be either https or http + if (Endpoint.Contains("http") && Endpoint.Contains("://") && Endpoint.Length > 7) + { + return true; + } + return false; + } + + public static bool ValidateICAOCode(string ICAO){ + if (ICAO.Length == 4) + { + //If the value contains a Number, then the value will return false + return !ICAO.Any(x => char.IsDigit(x)); + } + return false; + } + + public static bool ValidateCruiseAlt(int CruiseAlt){ + if (CruiseAlt > 0 && CruiseAlt < 50000) + { + return true; + } + return false; + } + } } \ No newline at end of file From d86378a9a85ee83af03a71f4953cd50ad51a23dc Mon Sep 17 00:00:00 2001 From: luke-else <52086083+luke-else@users.noreply.github.com> Date: Sun, 31 Oct 2021 16:36:14 +0000 Subject: [PATCH 05/15] Added APIInterface --- Controllers/API/APIInterface.cs | 80 +++++++++++++++++++ .../FormAuthenticator.cs | 2 +- EFB.csproj | 7 +- 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 Controllers/API/APIInterface.cs rename Controllers/{FormAuthenticator => Form}/FormAuthenticator.cs (96%) diff --git a/Controllers/API/APIInterface.cs b/Controllers/API/APIInterface.cs new file mode 100644 index 0000000..bfefc8f --- /dev/null +++ b/Controllers/API/APIInterface.cs @@ -0,0 +1,80 @@ +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.Net.Http; + +namespace EFB.Controllers.API +{ + public class APIInterface + { + private HttpClient HttpClient { get; set; } + + public async Task Get(string Endpoint, Dictionary Headers){ + + this.HttpClient = new HttpClient(); + + this.HttpClient.DefaultRequestHeaders.Clear(); + + foreach (var Header in Headers) + { + this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + } + + if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint)) + { + var pendingResult = this.HttpClient.GetAsync(Endpoint); + + var result = await pendingResult; + + string resultString = result.Content.ReadAsStringAsync().Result; + + return JsonConvert.DeserializeObject(resultString); + + }else{ + + T empty = default(T); + + return empty; + + } + + } + + public async Task Post(string Endpoint, Dictionary Headers, object Body){ + + this.HttpClient = new HttpClient(); + + this.HttpClient.DefaultRequestHeaders.Clear(); + + foreach (var Header in Headers) + { + this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + } + + StringContent content = new StringContent(JsonConvert.SerializeObject(Body), Encoding.UTF8, "application/json"); + + if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint)) + { + var pendingResult = this.HttpClient.PostAsync(Endpoint, content); + + var result = await pendingResult; + + string resultString = result.Content.ReadAsStringAsync().Result; + + return JsonConvert.DeserializeObject(resultString); + + }else{ + + T empty = default(T); + + return empty; + + } + + + } + + + } +} \ No newline at end of file diff --git a/Controllers/FormAuthenticator/FormAuthenticator.cs b/Controllers/Form/FormAuthenticator.cs similarity index 96% rename from Controllers/FormAuthenticator/FormAuthenticator.cs rename to Controllers/Form/FormAuthenticator.cs index 7038e8e..d9cc4f2 100644 --- a/Controllers/FormAuthenticator/FormAuthenticator.cs +++ b/Controllers/Form/FormAuthenticator.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace EFB.Controllers.FormAuthenticator +namespace EFB.Controllers.Form { public static class FormAuthenticator { diff --git a/EFB.csproj b/EFB.csproj index 842a770..1afd1da 100644 --- a/EFB.csproj +++ b/EFB.csproj @@ -1,7 +1,8 @@ - net5.0 - - + + + + \ No newline at end of file From a4a52d03839037cc2a3266d9ef1d8e8fc21b9435 Mon Sep 17 00:00:00 2001 From: luke-else <52086083+luke-else@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:58:30 +0000 Subject: [PATCH 06/15] Added User credential authentication --- Controllers/API/APIInterface.cs | 26 +++++++----- Controllers/UserController.cs | 74 +++++++++++++++++++++++++++++++++ Models/JSON/Login.cs | 19 +++++++++ Models/JSON/LoginResponse.cs | 28 +++++++++++++ Views/Home/Index.cshtml | 2 +- 5 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 Controllers/UserController.cs create mode 100644 Models/JSON/Login.cs create mode 100644 Models/JSON/LoginResponse.cs diff --git a/Controllers/API/APIInterface.cs b/Controllers/API/APIInterface.cs index bfefc8f..9977091 100644 --- a/Controllers/API/APIInterface.cs +++ b/Controllers/API/APIInterface.cs @@ -16,12 +16,15 @@ namespace EFB.Controllers.API this.HttpClient.DefaultRequestHeaders.Clear(); - foreach (var Header in Headers) + if (Headers != null) { - this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + foreach (var Header in Headers) + { + this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + } } - if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint)) + if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { var pendingResult = this.HttpClient.GetAsync(Endpoint); @@ -41,22 +44,23 @@ namespace EFB.Controllers.API } - public async Task Post(string Endpoint, Dictionary Headers, object Body){ + public async Task Post(string Endpoint, Dictionary Headers, HttpContent Body){ this.HttpClient = new HttpClient(); - this.HttpClient.DefaultRequestHeaders.Clear(); + //this.HttpClient.DefaultRequestHeaders.Clear(); - foreach (var Header in Headers) + if (Headers != null) { - this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + foreach (var Header in Headers) + { + this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + } } - StringContent content = new StringContent(JsonConvert.SerializeObject(Body), Encoding.UTF8, "application/json"); - - if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint)) + if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { - var pendingResult = this.HttpClient.PostAsync(Endpoint, content); + var pendingResult = this.HttpClient.PostAsync(Endpoint, Body); var result = await pendingResult; diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs new file mode 100644 index 0000000..02bab95 --- /dev/null +++ b/Controllers/UserController.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using System.Net.Http; +using EFB.Models.JSON; +using Microsoft.Extensions.Logging; + +namespace EFB.Controllers +{ + //[Route("[controller]")] + public class UserController : Controller + { + private readonly ILogger _logger; + + public UserController(ILogger logger) + { + _logger = logger; + } + + public IActionResult Index() + { + return View(); + } + + + public async Task Login(string email, string password){ + + if (Form.FormAuthenticator.ValidateEMail(email)) + { + //API Helper + API.APIInterface API = new API.APIInterface(); + + //Dictionary of Formdata to be encoded + Dictionary formData = new Dictionary(); + + formData.Add("grant_type", "client_credentials"); + formData.Add("client_id", email); + formData.Add("client_secret", password); + + HttpContent content = new FormUrlEncodedContent(formData); + + var request = API.Post("https://api.autorouter.aero/v1.0/oauth2/token", null, content); + + //Wait for the response to come through + var response = await request; + + if (response.error != null) + { + + TempData["Error"] = response.error_description; + return RedirectToAction("Index", "Home"); + + }else{ + //Create a user session and continue + return RedirectToAction("Index", "Home"); + } + + }else{ + TempData["Error"] = "Please enter a valid E-Mail"; + return RedirectToAction("Index", "Home"); + } + + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View("Error!"); + } + } +} \ No newline at end of file diff --git a/Models/JSON/Login.cs b/Models/JSON/Login.cs new file mode 100644 index 0000000..76db256 --- /dev/null +++ b/Models/JSON/Login.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Newtonsoft.Json; +using System.Threading.Tasks; + +namespace EFB.Models.JSON +{ + public class Login + { + [JsonProperty] + public string grant_type { get; set; } + [JsonProperty] + public string client_id { get; set; } + [JsonProperty] + public string client_secret { get; set; } + + } +} \ No newline at end of file diff --git a/Models/JSON/LoginResponse.cs b/Models/JSON/LoginResponse.cs new file mode 100644 index 0000000..765fac9 --- /dev/null +++ b/Models/JSON/LoginResponse.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace EFB.Models.JSON +{ + public class LoginResponse + { + [JsonProperty] + public string access_token { get; set; } + [JsonProperty] + public int expires_in { get; set; } + [JsonProperty] + public string token_type { get; set; } + [JsonProperty] + public string scope { get; set; } + + + [JsonProperty] + public string error { get; set; } = null; + [JsonProperty] + public string error_description { get; set; } = null; + + + } +} \ No newline at end of file diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index 6143668..930740a 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -11,7 +11,7 @@

-
-
- +
+
From 9a4f81ae1b91cda7b70c851e0ad7ae12b5433db6 Mon Sep 17 00:00:00 2001 From: lukejelse04 Date: Sun, 3 Oct 2021 22:35:59 +0100 Subject: [PATCH 09/15] Added Form Authenticator Includes the methods needed to verify the user inputs and to validate that they are correct. --- .../FormAuthenticator/FormAuthenticator.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Controllers/FormAuthenticator/FormAuthenticator.cs diff --git a/Controllers/FormAuthenticator/FormAuthenticator.cs b/Controllers/FormAuthenticator/FormAuthenticator.cs new file mode 100644 index 0000000..b7bef3c --- /dev/null +++ b/Controllers/FormAuthenticator/FormAuthenticator.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EFB.Controllers.FormAuthenticator +{ + public static class FormAuthenticator + { + + public static bool ValidateEMail(string EMail){ + if (EMail.Contains("@") && EMail.Contains(".") && !EMail.Contains(" ")) + { + if (EMail.Count(x => x == '@') == 1) + { + return true; + } + } + return false; + } + + + } +} \ No newline at end of file From dd72219fe2591a87e8405a5819f322920fde0839 Mon Sep 17 00:00:00 2001 From: lukejelse04 Date: Sun, 3 Oct 2021 22:37:17 +0100 Subject: [PATCH 10/15] Update FormAuthenticator.cs --- .../FormAuthenticator/FormAuthenticator.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Controllers/FormAuthenticator/FormAuthenticator.cs b/Controllers/FormAuthenticator/FormAuthenticator.cs index b7bef3c..7038e8e 100644 --- a/Controllers/FormAuthenticator/FormAuthenticator.cs +++ b/Controllers/FormAuthenticator/FormAuthenticator.cs @@ -19,6 +19,32 @@ namespace EFB.Controllers.FormAuthenticator return false; } + public static bool ValidateEndpoint(string Endpoint){ + //If it contains http & :// it can be either https or http + if (Endpoint.Contains("http") && Endpoint.Contains("://") && Endpoint.Length > 7) + { + return true; + } + return false; + } + + public static bool ValidateICAOCode(string ICAO){ + if (ICAO.Length == 4) + { + //If the value contains a Number, then the value will return false + return !ICAO.Any(x => char.IsDigit(x)); + } + return false; + } + + public static bool ValidateCruiseAlt(int CruiseAlt){ + if (CruiseAlt > 0 && CruiseAlt < 50000) + { + return true; + } + return false; + } + } } \ No newline at end of file From 2bf618f318fb151b172ce54dcee90d3ef4432299 Mon Sep 17 00:00:00 2001 From: luke-else <52086083+luke-else@users.noreply.github.com> Date: Sun, 31 Oct 2021 16:36:14 +0000 Subject: [PATCH 11/15] Added APIInterface --- Controllers/API/APIInterface.cs | 80 +++++++++++++++++++ .../FormAuthenticator.cs | 2 +- EFB.csproj | 7 +- 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 Controllers/API/APIInterface.cs rename Controllers/{FormAuthenticator => Form}/FormAuthenticator.cs (96%) diff --git a/Controllers/API/APIInterface.cs b/Controllers/API/APIInterface.cs new file mode 100644 index 0000000..bfefc8f --- /dev/null +++ b/Controllers/API/APIInterface.cs @@ -0,0 +1,80 @@ +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.Net.Http; + +namespace EFB.Controllers.API +{ + public class APIInterface + { + private HttpClient HttpClient { get; set; } + + public async Task Get(string Endpoint, Dictionary Headers){ + + this.HttpClient = new HttpClient(); + + this.HttpClient.DefaultRequestHeaders.Clear(); + + foreach (var Header in Headers) + { + this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + } + + if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint)) + { + var pendingResult = this.HttpClient.GetAsync(Endpoint); + + var result = await pendingResult; + + string resultString = result.Content.ReadAsStringAsync().Result; + + return JsonConvert.DeserializeObject(resultString); + + }else{ + + T empty = default(T); + + return empty; + + } + + } + + public async Task Post(string Endpoint, Dictionary Headers, object Body){ + + this.HttpClient = new HttpClient(); + + this.HttpClient.DefaultRequestHeaders.Clear(); + + foreach (var Header in Headers) + { + this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + } + + StringContent content = new StringContent(JsonConvert.SerializeObject(Body), Encoding.UTF8, "application/json"); + + if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint)) + { + var pendingResult = this.HttpClient.PostAsync(Endpoint, content); + + var result = await pendingResult; + + string resultString = result.Content.ReadAsStringAsync().Result; + + return JsonConvert.DeserializeObject(resultString); + + }else{ + + T empty = default(T); + + return empty; + + } + + + } + + + } +} \ No newline at end of file diff --git a/Controllers/FormAuthenticator/FormAuthenticator.cs b/Controllers/Form/FormAuthenticator.cs similarity index 96% rename from Controllers/FormAuthenticator/FormAuthenticator.cs rename to Controllers/Form/FormAuthenticator.cs index 7038e8e..d9cc4f2 100644 --- a/Controllers/FormAuthenticator/FormAuthenticator.cs +++ b/Controllers/Form/FormAuthenticator.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace EFB.Controllers.FormAuthenticator +namespace EFB.Controllers.Form { public static class FormAuthenticator { diff --git a/EFB.csproj b/EFB.csproj index 842a770..1afd1da 100644 --- a/EFB.csproj +++ b/EFB.csproj @@ -1,7 +1,8 @@ - net5.0 - - + + + + \ No newline at end of file From f630f07224908371b066670426fa63b7d89c8bf7 Mon Sep 17 00:00:00 2001 From: luke-else <52086083+luke-else@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:58:30 +0000 Subject: [PATCH 12/15] Added User credential authentication --- Controllers/API/APIInterface.cs | 26 +++++++----- Controllers/UserController.cs | 74 +++++++++++++++++++++++++++++++++ Models/JSON/Login.cs | 19 +++++++++ Models/JSON/LoginResponse.cs | 28 +++++++++++++ Views/Home/Index.cshtml | 2 +- 5 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 Controllers/UserController.cs create mode 100644 Models/JSON/Login.cs create mode 100644 Models/JSON/LoginResponse.cs diff --git a/Controllers/API/APIInterface.cs b/Controllers/API/APIInterface.cs index bfefc8f..9977091 100644 --- a/Controllers/API/APIInterface.cs +++ b/Controllers/API/APIInterface.cs @@ -16,12 +16,15 @@ namespace EFB.Controllers.API this.HttpClient.DefaultRequestHeaders.Clear(); - foreach (var Header in Headers) + if (Headers != null) { - this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + foreach (var Header in Headers) + { + this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + } } - if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint)) + if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { var pendingResult = this.HttpClient.GetAsync(Endpoint); @@ -41,22 +44,23 @@ namespace EFB.Controllers.API } - public async Task Post(string Endpoint, Dictionary Headers, object Body){ + public async Task Post(string Endpoint, Dictionary Headers, HttpContent Body){ this.HttpClient = new HttpClient(); - this.HttpClient.DefaultRequestHeaders.Clear(); + //this.HttpClient.DefaultRequestHeaders.Clear(); - foreach (var Header in Headers) + if (Headers != null) { - this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + foreach (var Header in Headers) + { + this.HttpClient.DefaultRequestHeaders.Add(Header.Key, Header.Value); + } } - StringContent content = new StringContent(JsonConvert.SerializeObject(Body), Encoding.UTF8, "application/json"); - - if (!Form.FormAuthenticator.ValidateEndpoint(Endpoint)) + if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { - var pendingResult = this.HttpClient.PostAsync(Endpoint, content); + var pendingResult = this.HttpClient.PostAsync(Endpoint, Body); var result = await pendingResult; diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs new file mode 100644 index 0000000..02bab95 --- /dev/null +++ b/Controllers/UserController.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using System.Net.Http; +using EFB.Models.JSON; +using Microsoft.Extensions.Logging; + +namespace EFB.Controllers +{ + //[Route("[controller]")] + public class UserController : Controller + { + private readonly ILogger _logger; + + public UserController(ILogger logger) + { + _logger = logger; + } + + public IActionResult Index() + { + return View(); + } + + + public async Task Login(string email, string password){ + + if (Form.FormAuthenticator.ValidateEMail(email)) + { + //API Helper + API.APIInterface API = new API.APIInterface(); + + //Dictionary of Formdata to be encoded + Dictionary formData = new Dictionary(); + + formData.Add("grant_type", "client_credentials"); + formData.Add("client_id", email); + formData.Add("client_secret", password); + + HttpContent content = new FormUrlEncodedContent(formData); + + var request = API.Post("https://api.autorouter.aero/v1.0/oauth2/token", null, content); + + //Wait for the response to come through + var response = await request; + + if (response.error != null) + { + + TempData["Error"] = response.error_description; + return RedirectToAction("Index", "Home"); + + }else{ + //Create a user session and continue + return RedirectToAction("Index", "Home"); + } + + }else{ + TempData["Error"] = "Please enter a valid E-Mail"; + return RedirectToAction("Index", "Home"); + } + + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View("Error!"); + } + } +} \ No newline at end of file diff --git a/Models/JSON/Login.cs b/Models/JSON/Login.cs new file mode 100644 index 0000000..76db256 --- /dev/null +++ b/Models/JSON/Login.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Newtonsoft.Json; +using System.Threading.Tasks; + +namespace EFB.Models.JSON +{ + public class Login + { + [JsonProperty] + public string grant_type { get; set; } + [JsonProperty] + public string client_id { get; set; } + [JsonProperty] + public string client_secret { get; set; } + + } +} \ No newline at end of file diff --git a/Models/JSON/LoginResponse.cs b/Models/JSON/LoginResponse.cs new file mode 100644 index 0000000..765fac9 --- /dev/null +++ b/Models/JSON/LoginResponse.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace EFB.Models.JSON +{ + public class LoginResponse + { + [JsonProperty] + public string access_token { get; set; } + [JsonProperty] + public int expires_in { get; set; } + [JsonProperty] + public string token_type { get; set; } + [JsonProperty] + public string scope { get; set; } + + + [JsonProperty] + public string error { get; set; } = null; + [JsonProperty] + public string error_description { get; set; } = null; + + + } +} \ No newline at end of file diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index 6143668..930740a 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -11,7 +11,7 @@

-
+
From d3970fba18c2ad090a5a38383370e5f8904064b9 Mon Sep 17 00:00:00 2001 From: luke-else <52086083+luke-else@users.noreply.github.com> Date: Sun, 7 Nov 2021 21:30:31 +0000 Subject: [PATCH 13/15] Updated API Interface Updated API Interface to incorporate a new response model in order to simplify responses and make error handling easier. --- Controllers/API/APIInterface.cs | 62 ++++++++++++++++++--------------- Models/ResponseModel.cs | 16 +++++++++ 2 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 Models/ResponseModel.cs diff --git a/Controllers/API/APIInterface.cs b/Controllers/API/APIInterface.cs index 9977091..f15712b 100644 --- a/Controllers/API/APIInterface.cs +++ b/Controllers/API/APIInterface.cs @@ -3,6 +3,7 @@ using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using System.Net.Http; +using EFB.Models; namespace EFB.Controllers.API { @@ -10,7 +11,7 @@ namespace EFB.Controllers.API { private HttpClient HttpClient { get; set; } - public async Task Get(string Endpoint, Dictionary Headers){ + public async Task Get(string Endpoint, Dictionary Headers){ this.HttpClient = new HttpClient(); @@ -26,29 +27,34 @@ namespace EFB.Controllers.API if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { - var pendingResult = this.HttpClient.GetAsync(Endpoint); + try + { + var pendingResult = this.HttpClient.GetAsync(Endpoint); - var result = await pendingResult; + var result = await pendingResult; - string resultString = result.Content.ReadAsStringAsync().Result; + string resultString = result.Content.ReadAsStringAsync().Result; - return JsonConvert.DeserializeObject(resultString); - - }else{ - - T empty = default(T); - - return empty; + return new ResponseModel{ + //Sender should be aware of type T becuase of Generic function + Result = JsonConvert.DeserializeObject(resultString) + }; + } + catch (System.Exception e) + { + return new ResponseModel{Error = e.Message}; + } } + //Returned in the event No other response has been returned + return new ResponseModel{Error = "Invalid Endpoint - Please try again later"}; } - public async Task Post(string Endpoint, Dictionary Headers, HttpContent Body){ + public async Task Post(string Endpoint, Dictionary Headers, HttpContent Body){ this.HttpClient = new HttpClient(); - - //this.HttpClient.DefaultRequestHeaders.Clear(); + this.HttpClient.DefaultRequestHeaders.Clear(); if (Headers != null) { @@ -60,25 +66,25 @@ namespace EFB.Controllers.API if (Form.FormAuthenticator.ValidateEndpoint(Endpoint)) { - var pendingResult = this.HttpClient.PostAsync(Endpoint, Body); - - var result = await pendingResult; - - string resultString = result.Content.ReadAsStringAsync().Result; - - return JsonConvert.DeserializeObject(resultString); - - }else{ - - T empty = default(T); - - return empty; + try{//Try statement to catch errors in the process of making the request + var pendingResult = this.HttpClient.PostAsync(Endpoint, Body); + var result = await pendingResult; + string resultString = result.Content.ReadAsStringAsync().Result; + return new ResponseModel{ + //Sender should be aware of type T becuase of Generic function + Result = JsonConvert.DeserializeObject(resultString) + }; + }catch(System.Exception e){ + return new ResponseModel{Error = e.Message}; + } } - + //Returned in the event No other response has been returned + return new ResponseModel{Error = "Invalid Endpoint - Please try again later"}; } } + } \ No newline at end of file diff --git a/Models/ResponseModel.cs b/Models/ResponseModel.cs new file mode 100644 index 0000000..debfb08 --- /dev/null +++ b/Models/ResponseModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EFB.Models +{ + public class ResponseModel + { + //Object should be of known type from sender + public object Result { get; set; } = null; + + public string Error { get; set; } = null; + + } +} \ No newline at end of file From c5f0478e082ebbeda6874916af0f54a87f2c0904 Mon Sep 17 00:00:00 2001 From: luke-else <52086083+luke-else@users.noreply.github.com> Date: Sun, 7 Nov 2021 22:06:16 +0000 Subject: [PATCH 14/15] Add Session Extensions --- Sessions/SessionExtensions.cs | 19 +++++++++++++++++++ Startup.cs | 3 +++ 2 files changed, 22 insertions(+) create mode 100644 Sessions/SessionExtensions.cs diff --git a/Sessions/SessionExtensions.cs b/Sessions/SessionExtensions.cs new file mode 100644 index 0000000..5b1577a --- /dev/null +++ b/Sessions/SessionExtensions.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; + +namespace EFB.Sessions +{ + public static class SessionExtensions + { + public static void SetObject(this ISession session, string key, object value) + {//Sets the object of a session to Object + session.SetString(key, JsonConvert.SerializeObject(value)); + } + + public static T GetObject(this ISession session, string key) + {//Gets a session of known type (T) + var value = session.GetString(key); + return value == null ? default(T) : JsonConvert.DeserializeObject(value); + } + } +} \ No newline at end of file diff --git a/Startup.cs b/Startup.cs index 1902a2e..90010f4 100644 --- a/Startup.cs +++ b/Startup.cs @@ -24,6 +24,7 @@ namespace EFB public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); + services.AddSession(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -44,6 +45,8 @@ namespace EFB app.UseRouting(); + app.UseSession(); + app.UseAuthorization(); app.UseEndpoints(endpoints => From 1ac94a533f848e04047c358cbaffd33077957be5 Mon Sep 17 00:00:00 2001 From: luke-else <52086083+luke-else@users.noreply.github.com> Date: Sun, 7 Nov 2021 22:08:46 +0000 Subject: [PATCH 15/15] Update User Model and Integrate new Sessions --- Controllers/UserController.cs | 38 ++++++++++++++++++++++++++++------- Models/UserModel.cs | 9 --------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index 02bab95..137160b 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -7,6 +7,8 @@ using Microsoft.AspNetCore.Mvc; using System.Net.Http; using EFB.Models.JSON; using Microsoft.Extensions.Logging; +using EFB.Models; +using EFB.Sessions; namespace EFB.Controllers { @@ -45,17 +47,39 @@ namespace EFB.Controllers var request = API.Post("https://api.autorouter.aero/v1.0/oauth2/token", null, content); //Wait for the response to come through - var response = await request; + ResponseModel response = await request; - if (response.error != null) + if (response.Error != null) { - - TempData["Error"] = response.error_description; + TempData["Error"] = response.Error; + TempData["email"] = email; return RedirectToAction("Index", "Home"); - }else{ - //Create a user session and continue - return RedirectToAction("Index", "Home"); + + //Type cast required but we know response will be of known type + LoginResponse login = (LoginResponse)response.Result; + + //Generate User Session + if (login.error == null) + { + UserModel user = new UserModel{ + EMail = email, + Token = new TokenModel{ + Token = login.access_token, + Expiration = DateTime.UtcNow.AddSeconds(login.expires_in) + } + }; + + //Using Session Extensions (Store the user session) + HttpContext.Session.SetObject("User", user); + return RedirectToAction("App", "Home"); + }else{ + TempData["Error"] = login.error_description; + TempData["email"] = email; + return RedirectToAction("Index", "Home"); + } + + } }else{ diff --git a/Models/UserModel.cs b/Models/UserModel.cs index 07b6785..e4e2dce 100644 --- a/Models/UserModel.cs +++ b/Models/UserModel.cs @@ -26,14 +26,5 @@ namespace EFB.Models //Contains the most recently stored position of the user in the simulator public object SimPosition { get; set; } = null; - - - - - - - - - } } \ No newline at end of file
+
From 92ffda865c6afb0c5f08de72582587f98b2b49f3 Mon Sep 17 00:00:00 2001 From: lukejelse04 Date: Mon, 20 Sep 2021 10:41:19 +0100 Subject: [PATCH 07/15] Created Login Page and update bootstrap files to allow for dark theme --- .vscode/launch.json | 35 + .vscode/tasks.json | 42 + Views/Home/Index.cshtml | 43 +- Views/Shared/_Layout.cshtml | 14 +- wwwroot/images/MainImage.png | Bin 0 -> 2821675 bytes .../bootstrap/dist/css/bootstrap-default.css | 10038 ++++++++++++++++ .../dist/css/bootstrap-default.min.css | 7 + wwwroot/lib/bootstrap/dist/css/bootstrap.css | 2336 ++-- .../lib/bootstrap/dist/css/bootstrap.min.css | 17 +- 9 files changed, 11583 insertions(+), 949 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 wwwroot/images/MainImage.png create mode 100644 wwwroot/lib/bootstrap/dist/css/bootstrap-default.css create mode 100644 wwwroot/lib/bootstrap/dist/css/bootstrap-default.min.css diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..0639eee --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,35 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/net5.0/EFB.dll", + "args": [], + "cwd": "${workspaceFolder}", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..a362bdd --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/EFB.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/EFB.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/EFB.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index d2d19bd..38a226b 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -1,8 +1,43 @@ @{ - ViewData["Title"] = "Home Page"; + ViewData["Title"] = "Welcome"; } -
-

Welcome

-

Learn about building Web apps with ASP.NET Core.

+
+ +
+
+

AutoRouter Login

+ +
+
+ + +
+ +
+
+ +
+ + + @{ + if (TempData["Error"] != null) + {//If an error has been flagged, information will be displayed to the user + +
+
+ +
+ Warning! @TempData["Error"]
+ } + } + +
+
+ +
+ +
+
diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index f41e91f..dcbba73 100644 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -9,9 +9,9 @@
-