AuthorizationController.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using IMCS.CCS.Common;
  2. using IMCS.CCS.Model;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using System.Threading.Tasks;
  6. namespace IMCS.CCS.Controllers
  7. {
  8. public class AuthorizationController : AppBaseController
  9. {
  10. private readonly IConfiguration _configuration;
  11. public AuthorizationController(IConfiguration configuration)
  12. {
  13. _configuration = configuration;
  14. }
  15. /// <summary>
  16. /// 检查 口令
  17. /// </summary>
  18. /// <param name="password"></param>
  19. /// <returns></returns>
  20. [HttpPost("check/{password}")]
  21. public async Task<ApiResult> CheckAsync(string password)
  22. {
  23. if (string.IsNullOrWhiteSpace(password))
  24. {
  25. return this.ResultWarn("请输入口令!");
  26. }
  27. var tokenKey = _configuration[AppConsts.TokenKeyName];
  28. var tokenValue = _configuration[AppConsts.TokenValueName];
  29. var jwtKeyName = _configuration[AppConsts.JwtKeyName];
  30. var jwtSecurityKey = _configuration[AppConsts.JwtSecurityKey];
  31. if (tokenValue != password)
  32. {
  33. return this.ResultWarn("口令错误!");
  34. }
  35. var token = JwtTokenUtil.CreateToken(tokenValue, jwtSecurityKey, jwtKeyName);
  36. const string tokenType = "Bearer ";
  37. return await Task.FromResult(this.ResultOk(new
  38. {
  39. tokenKey,
  40. token = tokenType + token
  41. }));
  42. }
  43. }
  44. }