TokenController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Business.Sqe;
  2. using Business.StructuredDB;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Configuration;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Runtime.InteropServices;
  12. using System.Security.Claims;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using Volo.Abp;
  16. using Volo.Abp.AspNetCore.Mvc;
  17. namespace Business.Controllers
  18. {
  19. [RemoteService]
  20. [Area("Sqe")]
  21. //[AuthAttribute]
  22. [Route("api/Token")]
  23. //[ApiController]
  24. public class TokenController : AbpController
  25. {
  26. public IJwtService _jwtService { set; get; }
  27. private IConfiguration _configuration;
  28. public TokenController(IJwtService jwtService, IConfiguration configuration)
  29. {
  30. _jwtService = jwtService;
  31. _configuration = configuration;
  32. }
  33. //[AllowAnonymous]
  34. [HttpGet]
  35. [Route("token")]
  36. //public async Task<IActionResult> GetToken()
  37. public String GetToken(String userid, String password)
  38. {
  39. ResultCode code = ResultCode.Fail, subCode = ResultCode.Fail;
  40. string subMsg = "";
  41. string data = "";
  42. try
  43. {
  44. JwtOptions jwtOptions = new JwtOptions();
  45. jwtOptions.ExpireSeconds = Convert.ToInt32(_configuration["JWT:ExpireSeconds"]);
  46. jwtOptions.Issuer = _configuration["JWT:Issuer"];
  47. jwtOptions.Audience = _configuration["JWT:Audience"];
  48. jwtOptions.Key = _configuration["JWT:Key"];
  49. String Userid = _configuration["jwtAuthorization:userid"];
  50. String Password = _configuration["jwtAuthorization:password"];
  51. if (Userid.CompareTo(userid) != 0 || Password.CompareTo(password) != 0)
  52. {
  53. data = "userid or password is wrong!";
  54. }
  55. else
  56. {
  57. List<Claim> claims = new List<Claim>();
  58. claims.Add(new Claim(ClaimTypes.Name, userid));
  59. data = _jwtService.BuildToken(claims, jwtOptions);
  60. if (!string.IsNullOrEmpty(data))
  61. {
  62. code = ResultCode.Success;
  63. subCode = ResultCode.Success;
  64. }
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. subMsg = ex.Message;
  70. code = ResultCode.Fail;
  71. subCode = ResultCode.Fail;
  72. }
  73. ResultViewModel result = ResultHelper.CreateResult(code, data, subCode, subMsg);
  74. return JsonConvert.SerializeObject(result);
  75. }
  76. //[HttpGet]
  77. //[Route("tokenCheck")]
  78. //public String CheckToken(string token)
  79. //{
  80. // ResultCode code = ResultCode.Fail, subCode = ResultCode.Fail;
  81. // string subMsg = "";
  82. // string data = "";
  83. // try
  84. // {
  85. // JwtOptions jwtOptions = new JwtOptions();
  86. // jwtOptions.ExpireSeconds = Convert.ToInt32(_configuration["JWT:ExpireSeconds"]);
  87. // jwtOptions.Issuer = _configuration["JWT:Issuer"];
  88. // jwtOptions.Audience = _configuration["JWT:Audience"];
  89. // jwtOptions.Key = _configuration["JWT:Key"];
  90. // data = _jwtService.ValidateToken(token, jwtOptions);
  91. // if (!string.IsNullOrEmpty(data))
  92. // {
  93. // code = ResultCode.Success;
  94. // subCode = ResultCode.Success;
  95. // }
  96. // }
  97. // catch (Exception ex)
  98. // {
  99. // subMsg = ex.Message;
  100. // code = ResultCode.Fail;
  101. // subCode = ResultCode.Fail;
  102. // }
  103. // ResultViewModel result = ResultHelper.CreateResult(code, data, subCode, subMsg);
  104. // return result.ToString();
  105. //}
  106. }
  107. }