0
0

AuthController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Threading.Tasks;
  2. using System.Security.Claims;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Admin.Core.Attributes;
  6. using Admin.Core.Model.Output;
  7. using Admin.Core.Service.Admin.Auth;
  8. using Admin.Core.Service.Admin.Auth.Input;
  9. using Admin.Core.Service.Admin.Auth.Output;
  10. using Admin.Core.Common.Auth;
  11. using System.Diagnostics;
  12. using Admin.Core.Service.Admin.LoginLog.Input;
  13. using Admin.Core.Service.Admin.LoginLog;
  14. namespace Admin.Core.Controllers.Admin
  15. {
  16. /// <summary>
  17. /// 授权管理
  18. /// </summary>
  19. public class AuthController : AreaController
  20. {
  21. private readonly IUserToken _userToken;
  22. private readonly IAuthService _authService;
  23. private readonly ILoginLogService _loginLogService;
  24. public AuthController(
  25. IUserToken userToken,
  26. IAuthService authServices,
  27. ILoginLogService loginLogService
  28. )
  29. {
  30. _userToken = userToken;
  31. _authService = authServices;
  32. _loginLogService = loginLogService;
  33. }
  34. /// <summary>
  35. /// 获取验证码
  36. /// </summary>
  37. /// <param name="lastKey">上次验证码键</param>
  38. /// <returns></returns>
  39. [HttpGet]
  40. [AllowAnonymous]
  41. [NoOprationLog]
  42. public async Task<IResponseOutput> GetVerifyCode(string lastKey)
  43. {
  44. return await _authService.GetVerifyCodeAsync(lastKey);
  45. }
  46. /// <summary>
  47. /// 获取密钥
  48. /// </summary>
  49. /// <returns></returns>
  50. [HttpGet]
  51. [AllowAnonymous]
  52. [NoOprationLog]
  53. public async Task<IResponseOutput> GetPassWordEncryptKey()
  54. {
  55. return await _authService.GetPassWordEncryptKeyAsync();
  56. }
  57. /// <summary>
  58. /// 查询用户信息
  59. /// </summary>
  60. /// <returns></returns>
  61. [HttpGet]
  62. [Login]
  63. public async Task<IResponseOutput> GetUserInfo()
  64. {
  65. return await _authService.GetUserInfoAsync();
  66. }
  67. /// <summary>
  68. /// 用户登录
  69. /// 根据登录信息生成Token
  70. /// </summary>
  71. /// <param name="input">登录信息</param>
  72. /// <returns></returns>
  73. [HttpPost]
  74. [AllowAnonymous]
  75. [NoOprationLog]
  76. public async Task<IResponseOutput> Login(AuthLoginInput input)
  77. {
  78. var sw = new Stopwatch();
  79. sw.Start();
  80. var res = (await _authService.LoginAsync(input)) as IResponseOutput;
  81. sw.Stop();
  82. #region 添加登录日志
  83. var loginLogAddInput = new LoginLogAddInput()
  84. {
  85. CreatedUserName = input.UserName,
  86. ElapsedMilliseconds = sw.ElapsedMilliseconds,
  87. Status = res.Success,
  88. Msg = res.Msg
  89. };
  90. AuthLoginOutput user = null;
  91. if (res.Success)
  92. {
  93. user = (res as IResponseOutput<AuthLoginOutput>).Data;
  94. loginLogAddInput.CreatedUserId = user.Id;
  95. loginLogAddInput.RealName = user.Name;
  96. }
  97. await _loginLogService.AddAsync(loginLogAddInput);
  98. #endregion
  99. if (!res.Success)
  100. {
  101. return res;
  102. }
  103. #region 生成token信息
  104. var token = _userToken.Build(new[]
  105. {
  106. new Claim(ClaimAttributes.UserId, user.Id.ToString()),
  107. new Claim(ClaimAttributes.UserName, user.UserName),
  108. new Claim(ClaimAttributes.UserRealName, user.Name)
  109. });
  110. #endregion
  111. return ResponseOutput.Ok(new { token });
  112. }
  113. }
  114. }