using MS.Microservice.Web.Application.Models.AccountRequests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using System.Net;
using System.Security.Claims;
using System.Text;
using MS.Microservice.Domain.Services.Interfaces;
using MS.Microservice.Core.Dto;
using MS.Microservice.Core.Functional;
using MS.Microservice.Infrastructure.Attributes;
using MS.Microservice.Core.Extension;
using MS.Microservice.Domain.Consts;
using MS.Microservice.Domain.Identity;
using MS.Microservice.Domain.Identity.Token;
using MS.Microservice.Web.Application.Identity;
using MS.Microservice.Web.Infrastructure.Http;
namespace MS.Microservice.Web.Controller;
[Route("api/v1/[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
private readonly SignInManager _signInManager;
private readonly ITokenGenerator _tokenGenerator;
private readonly IUserDomainService _userDomainService;
private readonly IUserPasswordService _userPasswordService;
private readonly IDistributedCache _cache;
public AccountController(SignInManager signInManager, ITokenGenerator tokenGenerator, IUserDomainService userDomainService, IUserPasswordService userPasswordService,
IDistributedCache cache)
{
_signInManager = signInManager;
_tokenGenerator = tokenGenerator;
_userDomainService = userDomainService;
_userPasswordService = userPasswordService;
_cache = cache;
}
///
/// 登录
///
///
///
[HttpPost("login")]
[ProducesResponseType(typeof(ResultDto), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ProblemDetails), (int)HttpStatusCode.BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), (int)HttpStatusCode.Unauthorized)]
[NoEncrypt]
public async Task Login([FromBody] LoginRequest request)
{
if (request.Account == null || request.Password.IsNullOrEmpty())
{
return this.ToProblem(Error.Validation("账号和密码不能为空。"));
}
byte[]? base64Buffer = null;
try
{
base64Buffer = Convert.FromBase64String(request.Password);
}
catch (FormatException)
{
return this.ToProblem(Error.Validation("密码传输格式无效。"));
}
var passworld = Encoding.UTF8.GetString(base64Buffer);
//查询用户
var user = await _userDomainService.FindAsync(request.Account);
if (user == null
|| user.IsTransient()
|| !await _userPasswordService.VerifyAndUpgradeAsync(user, passworld, HttpContext.RequestAborted))
{
return this.ToProblem(Error.Unauthorized(ExceptionConsts.AccountOrPasswordError));
}
//var user = new User(request.Account, request.Password, "", false, "18975152023", 1, 1, "marsonshine@163.com", "marsonshine", "", "");
//user.Id = 1;
//user.AddRole(new Role(1, "Administrator", "管理员"));
// appservice
var token = await _tokenGenerator.Generate(user);
var result = new AuthenticateResult(user, token);
//await _signInManager.SignInAsync(user, false);
return Ok(new ResultDto(result));
}
///
/// 授权(获取用户有权限的url)
///
///
[HttpGet("auth")]
[Authorize(Policy = "Manage")]
[ProducesResponseType(typeof(ResultDto), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.OK)]
[NoEncrypt]
public async Task Auth()
{
var identity = new ClaimsIdentity("BearerIdentity");
identity.AddClaims(User.Claims);
var ju = UserClaimHelper.JWT2User(identity);
var user = await _userDomainService.FindAsync(ju.Account!);
return Ok(new Domain.Identity.ActionResult(user!));
}
}