57 lines
1.4 KiB
C#
57 lines
1.4 KiB
C#
|
namespace WebApi.Controllers;
|
|||
|
|
|||
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
using Newtonsoft.Json.Linq;
|
|||
|
using NuGet.Common;
|
|||
|
using Parking_spaces.Authorization;
|
|||
|
using Parking_spaces.Models;
|
|||
|
using Parking_spaces.Services;
|
|||
|
|
|||
|
[ApiController]
|
|||
|
[Authorize]
|
|||
|
[Route("[controller]")]
|
|||
|
public class UsersController : ControllerBase
|
|||
|
{
|
|||
|
private IUserService _userService;
|
|||
|
|
|||
|
public UsersController(IUserService userService)
|
|||
|
{
|
|||
|
_userService = userService;
|
|||
|
}
|
|||
|
|
|||
|
[AllowAnonymous]
|
|||
|
[HttpPost("authenticate")]
|
|||
|
public IActionResult Authenticate(AuthenticateRequest model)
|
|||
|
{
|
|||
|
var response = _userService.Authenticate(model);
|
|||
|
|
|||
|
if (response == null)
|
|||
|
return BadRequest(new { message = "Username or password is incorrect" });
|
|||
|
|
|||
|
// 将令牌添加到响应头中
|
|||
|
Response.Headers.Add("Authorization", "Bearer " + response.Token);
|
|||
|
|
|||
|
// 将令牌保存在Cookie或其他适当的位置
|
|||
|
Response.Cookies.Append("token", response.Token);
|
|||
|
return Ok(response);
|
|||
|
// 重定向到另一个页面
|
|||
|
//return RedirectToAction("/Park_spaces/Parking_spaces_total_table");
|
|||
|
//return RedirectToAction("Parking_spaces_total_table", "Park_spaces");
|
|||
|
}
|
|||
|
|
|||
|
[HttpGet]
|
|||
|
public IActionResult GetAll()
|
|||
|
{
|
|||
|
var users = _userService.GetAll();
|
|||
|
return Ok(users);
|
|||
|
}
|
|||
|
|
|||
|
[HttpGet("token")]
|
|||
|
public IActionResult Token()
|
|||
|
{
|
|||
|
|
|||
|
return Ok();
|
|||
|
}
|
|||
|
|
|||
|
}
|