From 7cbc870f78365567a760da35c0c590b5aec91a12 Mon Sep 17 00:00:00 2001 From: Mohd Ariff Date: Mon, 9 Dec 2024 16:28:42 +0800 Subject: [PATCH] Update --- Areas/Identity/Pages/Account/Login.cshtml | 80 +++++++++++++++++++ Controllers/API/AdminAPI.cs | 93 +++++++++++++++++++++++ Controllers/API/IdentityAPI.cs | 11 +-- Views/Admin/UserAdmin.cshtml | 60 ++++----------- 4 files changed, 192 insertions(+), 52 deletions(-) diff --git a/Areas/Identity/Pages/Account/Login.cshtml b/Areas/Identity/Pages/Account/Login.cshtml index 6354164..2ab7656 100644 --- a/Areas/Identity/Pages/Account/Login.cshtml +++ b/Areas/Identity/Pages/Account/Login.cshtml @@ -46,6 +46,26 @@ +
+
+

Use a local account to log in.

+
+ +
+ + + +
+
+ + + +
+
+ +
+
+

Use another service to log in.

@@ -80,4 +100,64 @@ @section Scripts { + + } diff --git a/Controllers/API/AdminAPI.cs b/Controllers/API/AdminAPI.cs index 2c029aa..70cb4af 100644 --- a/Controllers/API/AdminAPI.cs +++ b/Controllers/API/AdminAPI.cs @@ -4,6 +4,10 @@ using Microsoft.EntityFrameworkCore; using PSTW_CentralSystem.DBContext; using PSTW_CentralSystem.Models; using System.Reflection; +using static System.Runtime.InteropServices.JavaScript.JSType; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; namespace PSTW_CentralSystem.Controllers.API { @@ -15,6 +19,8 @@ namespace PSTW_CentralSystem.Controllers.API private readonly ILogger _logger; private readonly IdentityDBContext _authDbContext; private readonly UserManager _userManager; + // Communication Key for API. Not API authentication key + private readonly string _commKey = "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF4NW42anlkNlpTYzZNSE1Zem9qaApUbldpYTIra2pud2ZNbVhpSWlyK0RadjM2cEVGMGhRUWFLaWpaMWtyMGNiT25Ha2d2QnNwTzNiYkFua0E3SWwzCk4zM3NNYWdQV0JOQzZyVm1jT04zNEhDSWJCM0hvQXFYQUtkSHFUOGZneklMRzFhRzdxK2h4RDZhZzZsemhTMnEKdDA1bHdNc0hONWpOdmVNNnFWalRnTVB4aEFOMUhnUTkrd1lRWFh5bnZYYUo5OUNySHBqS21WdUt2VUh6WXdlRwp6SnBtYXZOclc4bE9oM1lMeVNuUVU5bjRrdURubGc1OWNHeUtKbzJ2YUxZbll4MkR1ZDNabzBXMHRMWGd0dlQyCjVXdVFsY0NVbldvaVpBV1JBTGI3anRpcTF0MGY5eVBiV2gxYXpMMjFoL3QvckJUMXNCL2FQd2kzRCt3MnBUR00KeVFJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg=="; public AdminAPI(ILogger logger, IdentityDBContext authDbContext, UserManager userManager) { @@ -117,6 +123,93 @@ namespace PSTW_CentralSystem.Controllers.API return StatusCode(500, $"An error occurred: {ex.Message}"); } } + public class LdapLoginCredential + { + public required string username { get; set; } + public required string password { get; set; } + } + [HttpPost("LdapLogin")] + public async Task LdapLogin([FromBody] LdapLoginCredential ldapLoginInfo) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + byte[] noFormatString = Convert.FromBase64String(_commKey); + string initUrlKey = Encoding.UTF8.GetString(noFormatString); + + string jsonData = JsonSerializer.Serialize(ldapLoginInfo); + + RSA rsaBase = RSA.Create(); + rsaBase.ImportFromPem(initUrlKey.ToCharArray()); + byte[] rsaData = rsaBase.Encrypt(Encoding.UTF8.GetBytes(jsonData), RSAEncryptionPadding.Pkcs1); + string rsaDataBase64 = Convert.ToBase64String(rsaData); + + Console.WriteLine("Sending data (RSA-Encrypted JSON as Base64)"); + Console.WriteLine(rsaDataBase64); + Console.WriteLine(""); + + string ldapUrl = "http://192.168.11.231/api/ldap/"; + string ldapUrlResult = ""; + + using (HttpClient httpClient = new HttpClient()) + { + try + { + StringContent rsaDataB64HttpContent = new(rsaDataBase64, Encoding.UTF8); + HttpResponseMessage ldapUrlResponse = httpClient.PostAsync(ldapUrl, rsaDataB64HttpContent).Result; + ldapUrlResponse.EnsureSuccessStatusCode(); + if (ldapUrlResponse.IsSuccessStatusCode) + { + ldapUrlResult = ldapUrlResponse.Content.ReadAsStringAsync().Result; + } + } + catch (Exception e) + { + return BadRequest(new { Message = $"Message: {e.Message}\nException Caught!" }); + } + } + + + userLdapInfo userInfo = JsonSerializer.Deserialize(ldapUrlResult)!; + + if (userInfo.Authenticated != "True") + { + return BadRequest(new { Message = "Login Failed" }); + } + + UserModel ldapuser = new UserModel() + { + UserName = userInfo.UserInfo.Email, + Email = userInfo.UserInfo.Email, + }; + + return Json(userInfo); + } + + class userLdapInfo() + { + public required string Authenticated { get; set; } + public required userInfo UserInfo { get; set; } + } + class userInfo() + { + public required string FirstName { get; set; } + public required string LastName { get; set; } + public required string DisplayName { get; set; } + public required string Description { get; set; } + public required string Username { get; set; } + public required string Office { get; set; } + public required string Email { get; set; } + public required string Street { get; set; } + public required string City { get; set; } + public required string State { get; set; } + public required string ZipCode { get; set; } + public required string Country { get; set; } + public required string Home { get; set; } + public required string Mobile { get; set; } + + } } } diff --git a/Controllers/API/IdentityAPI.cs b/Controllers/API/IdentityAPI.cs index f77ca74..f57f152 100644 --- a/Controllers/API/IdentityAPI.cs +++ b/Controllers/API/IdentityAPI.cs @@ -35,11 +35,12 @@ namespace PSTW_CentralSystem.Controllers.API var userInfo = await _authDbContext.Users.Include(u => u.Department).Select(u => new { - u.Id, - u.NormalizedEmail, - u.Department, - userRole, - }).Where(u => u.Id == user.Id).FirstOrDefaultAsync(); + id = u.Id, + email = u.NormalizedEmail, + company = u.Department!.Company!.CompanyName, + department =u.Department, + role = userRole, + }).Where(u => u.id == user.Id).FirstOrDefaultAsync(); if (userInfo == null) { diff --git a/Views/Admin/UserAdmin.cshtml b/Views/Admin/UserAdmin.cshtml index 3a8a414..c8b8d97 100644 --- a/Views/Admin/UserAdmin.cshtml +++ b/Views/Admin/UserAdmin.cshtml @@ -7,7 +7,7 @@ }

- Create New + @* Create New *@

@@ -122,69 +122,35 @@ initiateTable() { self = this; this.itemDatatable = $('#userDatatable').DataTable({ - "data": this.items, + "data": this.userList, "columns": [ { - "title": "Unique Id", - "data": "uniqueID", + "title": "UID", + "data": "id", "createdCell": function (td, cellData, rowData, row, col) { // Assign a unique ID to the element $(td).attr('id', `qr${cellData}`); }, }, { - "title": "Serial Number", - "data": "serialNumber", + "title": "Email", + "data": "email", }, { - "title": "Quantity", - "data": "quantity", + "title": "Company Name", + "data": "company", }, { - "title": "Supplier", - "data": "supplier", + "title": "Department", + "data": "department", }, { - "title": "Purchase Date", - "data": "purchaseDate", - }, - { - "title": "Price After Convert(RM)", - "data": "convertPrice", - }, - { - "title": "Invoice Date", - "data": "invoiceDate", - }, - { - "title": "Warranty Until", - "data": "warranty", - "render": function (data, type, full, meta) { - if (data > 0) { return full.endWDate } - else { return data } - } - }, - // { - // "title": "Image", - // "data": "imageProduct", - // "render": function (data, type, full, meta) { - // var image = ` - // Image - // `; - // return image; - // }, - // }, - { - "title": "Print", - "data": "uniqueID", - "render": function (data) { - var printButton = ``; - return printButton; - }, + "title": "Role", + "data": "role", }, { "title": "Delete", - "data": "productId", + "data": "id", "render": function (data) { var deleteButton = ``; return deleteButton;