diff --git a/TCM_API/TCM_API/Authorization/JwtUtils.cs b/TCM_API/TCM_API/Authorization/JwtUtils.cs index b6e1ebf..20f1b38 100644 --- a/TCM_API/TCM_API/Authorization/JwtUtils.cs +++ b/TCM_API/TCM_API/Authorization/JwtUtils.cs @@ -33,7 +33,13 @@ public class JwtUtils : IJwtUtils var key = Encoding.ASCII.GetBytes(_appSettings.Secret!); var tokenDescriptor = new SecurityTokenDescriptor { - Subject = new ClaimsIdentity(new[] { new Claim("id", user.id.ToString()) }), + Subject = new ClaimsIdentity(new[] + { + new Claim("id", user.id.ToString()), + new Claim("firstname", user.firstname ?? ""), // 加入 firstname + new Claim("lastname", user.lastname ?? ""), // 加入 lastname + new Claim("level", user.level ?? "") // 加入 lastname + }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; diff --git a/TCM_API/TCM_API/Controllers/UsersController.cs b/TCM_API/TCM_API/Controllers/UsersController.cs index 98b5212..6854df2 100644 --- a/TCM_API/TCM_API/Controllers/UsersController.cs +++ b/TCM_API/TCM_API/Controllers/UsersController.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using NuGet.Common; +using System.IdentityModel.Tokens.Jwt; using TCM_API.Authorization; using TCM_API.Models; using TCM_API.Services; @@ -38,6 +39,43 @@ public class UsersController : ControllerBase //return RedirectToAction("/Park_spaces/Parking_spaces_total_table"); //return RedirectToAction("Parking_spaces_total_table", "Park_spaces"); } + [HttpGet("token_check")] + public IActionResult Token() + { + return Ok(); + } + + + + [HttpGet("token_check_user")] + public IActionResult GetUserData() + { + var tokenStr = HttpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", ""); + + if (string.IsNullOrEmpty(tokenStr)) + { + return Unauthorized("Token is missing or invalid."); + } + + var tokenHandler = new JwtSecurityTokenHandler(); + try + { + var token = tokenHandler.ReadJwtToken(tokenStr); + + // 轉換 payload 為字典 + var payloadData = token.Payload + .ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToString()); + + // 回傳 payload 作為 JSON + return Ok(payloadData); + } + catch (Exception ex) + { + return BadRequest($"Error parsing token: {ex.Message}"); + } + } + + [HttpGet] public IActionResult GetAll() @@ -46,11 +84,10 @@ public class UsersController : ControllerBase return Ok(users); } - [HttpGet("token")] - public IActionResult Token() - { + //[HttpPost("create_manage")] + + + - return Ok(); - } } diff --git a/TCM_API/TCM_API/Entities/User.cs b/TCM_API/TCM_API/Entities/User.cs index 8afe8df..93d930c 100644 --- a/TCM_API/TCM_API/Entities/User.cs +++ b/TCM_API/TCM_API/Entities/User.cs @@ -8,7 +8,9 @@ public class User public int id { get; set; } public string? firstname { get; set; } public string? lastname { get; set; } + public string? email { get; set; } public string? username { get; set; } + public string? level { get; set; } [JsonIgnore] public string? password { get; set; } diff --git a/TCM_API/TCM_API/Models/AuthenticateResponse.cs b/TCM_API/TCM_API/Models/AuthenticateResponse.cs index 9acade3..83eff3e 100644 --- a/TCM_API/TCM_API/Models/AuthenticateResponse.cs +++ b/TCM_API/TCM_API/Models/AuthenticateResponse.cs @@ -8,6 +8,8 @@ public class AuthenticateResponse public string? firstname { get; set; } public string? lastname { get; set; } public string? username { get; set; } + public string? email { get; set; } + public string? level { get; set; } public string Token { get; set; } @@ -17,6 +19,8 @@ public class AuthenticateResponse firstname = user.firstname; lastname = user.lastname; username = user.username; + level = user.level; + email = user.email; Token = token; } } \ No newline at end of file diff --git a/TCM_API/TCM_API/Services/UserService.cs b/TCM_API/TCM_API/Services/UserService.cs index a94e32c..af9994e 100644 --- a/TCM_API/TCM_API/Services/UserService.cs +++ b/TCM_API/TCM_API/Services/UserService.cs @@ -41,11 +41,13 @@ public class UserService : IUserService private readonly SqlContext _dbContext; - + public AuthenticateResponse? Authenticate(AuthenticateRequest model) { - var user = _dbContext.user_table.SingleOrDefault(x => x.username == model.Username && x.password == model.Password); + var user = _dbContext.user_table.SingleOrDefault( + x => (x.username == model.Username || x.email == model.Username) + && x.password == model.Password); // return null if user not found if (user == null) return null; @@ -56,6 +58,7 @@ public class UserService : IUserService return new AuthenticateResponse(user, token); } + public IEnumerable GetAll() { return _dbContext.user_table; diff --git a/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.dll b/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.dll index ba78c83..8a0e018 100644 Binary files a/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.dll and b/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.dll differ diff --git a/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.exe b/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.exe index 7e5360d..39bfde9 100644 Binary files a/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.exe and b/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.exe differ diff --git a/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.pdb b/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.pdb index 519a804..3267f58 100644 Binary files a/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.pdb and b/TCM_API/TCM_API/bin/Debug/net8.0/TCM_API.pdb differ diff --git a/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.dll b/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.dll index 3083d10..a5f603c 100644 Binary files a/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.dll and b/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.dll differ diff --git a/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.exe b/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.exe index 7e5360d..39bfde9 100644 Binary files a/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.exe and b/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.exe differ diff --git a/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.pdb b/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.pdb index 6f3bcfa..66eeebe 100644 Binary files a/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.pdb and b/TCM_API/TCM_API/bin/Release/net8.0/TCM_API.pdb differ diff --git a/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.AssemblyInfo.cs b/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.AssemblyInfo.cs index 92f5fef..74cf5b6 100644 --- a/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.AssemblyInfo.cs +++ b/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.AssemblyInfo.cs @@ -15,7 +15,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("TCM_API")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ece8ee57edb0d2493f481f1c8d50e26c9e16e4c6")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+da6bf1a5d8e3be40169bb567a42fc986996f5828")] [assembly: System.Reflection.AssemblyProductAttribute("TCM_API")] [assembly: System.Reflection.AssemblyTitleAttribute("TCM_API")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.AssemblyInfoInputs.cache b/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.AssemblyInfoInputs.cache index 28e3d5a..add7744 100644 --- a/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.AssemblyInfoInputs.cache +++ b/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.AssemblyInfoInputs.cache @@ -1 +1 @@ -acd3dc0f3a7f727612bb4920a5c232cc324858045ed6a056a0aca5dd3f2cd4ed +b157c2f75f8f12bc057e7412b716f133c99ed1422d4ae74c155c658816d83d4f diff --git a/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.dll b/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.dll index ba78c83..8a0e018 100644 Binary files a/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.dll and b/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.dll differ diff --git a/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.pdb b/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.pdb index 519a804..3267f58 100644 Binary files a/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.pdb and b/TCM_API/TCM_API/obj/Debug/net8.0/TCM_API.pdb differ diff --git a/TCM_API/TCM_API/obj/Debug/net8.0/apphost.exe b/TCM_API/TCM_API/obj/Debug/net8.0/apphost.exe index 7e5360d..39bfde9 100644 Binary files a/TCM_API/TCM_API/obj/Debug/net8.0/apphost.exe and b/TCM_API/TCM_API/obj/Debug/net8.0/apphost.exe differ diff --git a/TCM_API/TCM_API/obj/Debug/net8.0/ref/TCM_API.dll b/TCM_API/TCM_API/obj/Debug/net8.0/ref/TCM_API.dll index b742637..22c2a9b 100644 Binary files a/TCM_API/TCM_API/obj/Debug/net8.0/ref/TCM_API.dll and b/TCM_API/TCM_API/obj/Debug/net8.0/ref/TCM_API.dll differ diff --git a/TCM_API/TCM_API/obj/Debug/net8.0/refint/TCM_API.dll b/TCM_API/TCM_API/obj/Debug/net8.0/refint/TCM_API.dll index b742637..22c2a9b 100644 Binary files a/TCM_API/TCM_API/obj/Debug/net8.0/refint/TCM_API.dll and b/TCM_API/TCM_API/obj/Debug/net8.0/refint/TCM_API.dll differ diff --git a/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.AssemblyInfo.cs b/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.AssemblyInfo.cs index cb3ff8c..9173fcf 100644 --- a/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.AssemblyInfo.cs +++ b/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.AssemblyInfo.cs @@ -15,7 +15,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("TCM_API")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Release")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ece8ee57edb0d2493f481f1c8d50e26c9e16e4c6")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+da6bf1a5d8e3be40169bb567a42fc986996f5828")] [assembly: System.Reflection.AssemblyProductAttribute("TCM_API")] [assembly: System.Reflection.AssemblyTitleAttribute("TCM_API")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.AssemblyInfoInputs.cache b/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.AssemblyInfoInputs.cache index 58761cd..ba73d2a 100644 --- a/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.AssemblyInfoInputs.cache +++ b/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.AssemblyInfoInputs.cache @@ -1 +1 @@ -67ba2fdcf3d9cd68d72b04e093aa3069eeed8893ad28042789943e1d36b9d86c +c3d4cf475f301f99dc3d6620f1cc3791186bd2882483e0e65597a75f84af99f5 diff --git a/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.dll b/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.dll index 3083d10..a5f603c 100644 Binary files a/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.dll and b/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.dll differ diff --git a/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.pdb b/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.pdb index 6f3bcfa..66eeebe 100644 Binary files a/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.pdb and b/TCM_API/TCM_API/obj/Release/net8.0/TCM_API.pdb differ diff --git a/TCM_API/TCM_API/obj/Release/net8.0/apphost.exe b/TCM_API/TCM_API/obj/Release/net8.0/apphost.exe index 7e5360d..39bfde9 100644 Binary files a/TCM_API/TCM_API/obj/Release/net8.0/apphost.exe and b/TCM_API/TCM_API/obj/Release/net8.0/apphost.exe differ diff --git a/TCM_API/TCM_API/obj/Release/net8.0/ref/TCM_API.dll b/TCM_API/TCM_API/obj/Release/net8.0/ref/TCM_API.dll index d035d95..9e079d8 100644 Binary files a/TCM_API/TCM_API/obj/Release/net8.0/ref/TCM_API.dll and b/TCM_API/TCM_API/obj/Release/net8.0/ref/TCM_API.dll differ diff --git a/TCM_API/TCM_API/obj/Release/net8.0/refint/TCM_API.dll b/TCM_API/TCM_API/obj/Release/net8.0/refint/TCM_API.dll index d035d95..9e079d8 100644 Binary files a/TCM_API/TCM_API/obj/Release/net8.0/refint/TCM_API.dll and b/TCM_API/TCM_API/obj/Release/net8.0/refint/TCM_API.dll differ