AuthController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 
  2. using Microsoft.Extensions.Caching.Distributed;
  3. using OASystem.Infrastructure.Repositories.Login;
  4. using System.IdentityModel.Tokens.Jwt;
  5. using System.Security.Claims;
  6. using StackExchange.Redis;
  7. namespace OASystem.API.Controllers
  8. {
  9. /// <summary>
  10. /// 鉴权相关
  11. /// </summary>
  12. public class AuthController : ControllerBase
  13. {
  14. private readonly IConfiguration _config;
  15. private readonly LoginRepository _loginRep;
  16. private readonly IMapper _mapper;
  17. private IDatabase _redis;
  18. private RedisHelper _redisHelper;
  19. public AuthController(IConfiguration config, LoginRepository loginRep, IMapper mapper, RedisHelper client)
  20. {
  21. _config = config;
  22. _loginRep = loginRep;
  23. _mapper = mapper;
  24. //_redis = client.GetDatabase(RedisEnum.Common);
  25. //_redisHelper = client("132.232.92.186", "7369", "123456");
  26. }
  27. /// <summary>
  28. /// 用户登录
  29. /// </summary>
  30. /// <param name="dto"></param>
  31. /// <returns></returns>
  32. [AllowAnonymous]
  33. [HttpPost("login")]
  34. [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
  35. public async Task<IActionResult> LoginAsync(LoginDto dto)
  36. {
  37. #region 校验用户信息,假设此处我们已经校验成功
  38. var userData = OAMethodLib.LoginLib.Api_Login(_loginRep,dto);
  39. if (userData.Result.Code != 0)
  40. {
  41. if (userData.Result.Code != 0) { return Ok(JsonView(false, userData.Result.Message)); }
  42. return Ok(JsonView(false,"暂无该员工信息!"));
  43. }
  44. #endregion
  45. var view = new LoginView
  46. {
  47. Expires = DateTime.Now.AddMinutes(30)
  48. };
  49. string authorId = dto.Number + "Token";
  50. var claims = new[] { new Claim(ClaimTypes.NameIdentifier, "Future") };
  51. var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"]));
  52. var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
  53. var token = new JwtSecurityToken(
  54. issuer: "OASystem.com",
  55. audience: "OASystem.com",
  56. claims: claims,
  57. expires: view.Expires,
  58. signingCredentials: creds);
  59. view.Token = new JwtSecurityTokenHandler().WriteToken(token);
  60. // 往Redis里面存入数据
  61. _redis.StringSet(authorId, view.Token);
  62. // 从Redis里面取数据
  63. string name = _redis.StringGet(authorId);
  64. return Ok(JsonView(view));
  65. }
  66. /// <summary>
  67. /// 测试auth
  68. /// </summary>
  69. /// <param name="dto"></param>
  70. /// <returns></returns>
  71. [Authorize]
  72. [HttpPost("TestToken")]
  73. [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
  74. public async Task<IActionResult> TestToken(LoginDto dto)
  75. {
  76. string authorId = dto.Number + "Token";
  77. // 从Redis里面取数据
  78. string userToken = _redis.StringGet(authorId);
  79. var view = new LoginView
  80. {
  81. Token = authorId + ":" + userToken
  82. };
  83. return Ok(JsonView(view));
  84. }
  85. }
  86. }