添加项目文件。
This commit is contained in:
1 parent
6a017c9547
commit
58abbb5b4f
86 files changed
+8729
No files matched your search
@@ -0,0 +1,112 @@
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using StoreHub.Framework.Commons;
|
||||
using StoreHub.Framework.Security;
|
||||
using StoreHub.Services;
|
||||
using StoreHub.Services.Dtos;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace StoreHub.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("1.0")]
|
||||
[Route("api/v{version:apiVersion}/[controller]/[action]")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
private readonly ITokenService _tokenService;
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly IOnlineUserStore _onlineUserStore;
|
||||
|
||||
public AuthController(IUserService userService, ITokenService tokenService, ICurrentUser currentUser, IOnlineUserStore onlineUserStore)
|
||||
{
|
||||
_userService = userService;
|
||||
_tokenService = tokenService;
|
||||
_currentUser = currentUser;
|
||||
_onlineUserStore = onlineUserStore;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 登录:校验用户名/密码,签发 JWT(单点登录:同用户的新登录会使旧令牌失效)
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
[AllowAnonymous]
|
||||
public async Task<ApiResult<object?>> Login([FromBody] LoginRequest request)
|
||||
{
|
||||
if (request is null || string.IsNullOrWhiteSpace(request.UserName) || string.IsNullOrWhiteSpace(request.Password))
|
||||
{
|
||||
return ApiResult.Error("用户名或密码不能为空", 400);
|
||||
}
|
||||
|
||||
UserDto? user = await _userService.LoginAsync(request.UserName, request.Password);
|
||||
if (user is null)
|
||||
{
|
||||
return ApiResult.Error("用户名或密码错误", 401);
|
||||
}
|
||||
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new(JwtClaimTypes.UserId, user.Id.ToString()),
|
||||
new(JwtClaimTypes.UserName, user.UserName),
|
||||
new(JwtClaimTypes.NickName, user.NickName),
|
||||
};
|
||||
|
||||
TokenResult token = _tokenService.CreateToken(claims);
|
||||
return ApiResult.Success<object?>(token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 退出登录:销毁当前用户的服务端在线会话,令牌立即失效
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
public ApiResult<object?> Logout()
|
||||
{
|
||||
if (_currentUser.Id is int userId)
|
||||
{
|
||||
_onlineUserStore.SignOut(userId);
|
||||
}
|
||||
|
||||
return ApiResult.Success<object?>(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前登录用户信息(需携带 Bearer Token)
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public ApiResult<object?> Me()
|
||||
{
|
||||
return ApiResult.Success<object?>(new
|
||||
{
|
||||
UserId = _currentUser.Id,
|
||||
UserName = _currentUser.UserName,
|
||||
NickName = _currentUser.NickName,
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在线人员列表(需携带 Bearer Token)
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public ApiResult<object?> OnlineUsers()
|
||||
{
|
||||
IReadOnlyList<OnlineUserInfo> users = _onlineUserStore.GetOnlineUsers();
|
||||
return ApiResult.Success<object?>(new
|
||||
{
|
||||
Count = users.Count,
|
||||
Users = users,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public class LoginRequest
|
||||
{
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user