UP0121
This commit is contained in:
parent
da6bf1a5d8
commit
c8c90fe0dc
@ -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)
|
||||
};
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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; }
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<User> GetAll()
|
||||
{
|
||||
return _dbContext.user_table;
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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")]
|
||||
|
@ -1 +1 @@
|
||||
acd3dc0f3a7f727612bb4920a5c232cc324858045ed6a056a0aca5dd3f2cd4ed
|
||||
b157c2f75f8f12bc057e7412b716f133c99ed1422d4ae74c155c658816d83d4f
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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")]
|
||||
|
@ -1 +1 @@
|
||||
67ba2fdcf3d9cd68d72b04e093aa3069eeed8893ad28042789943e1d36b9d86c
|
||||
c3d4cf475f301f99dc3d6620f1cc3791186bd2882483e0e65597a75f84af99f5
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user