LoginController.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using QM.AuthServer;
  5. using QM.AuthServer.Auth;
  6. using QM.AuthServer.Models.Users;
  7. using System.Collections.Generic;
  8. using System.Net.Http;
  9. using System.Threading.Tasks;
  10. using WS;
  11. using WS.AutoMapper;
  12. using WS.Helper;
  13. using WS.Image;
  14. using WS.WebCore.Api;
  15. namespace QM.Gateway.Controllers
  16. {
  17. /// <summary>
  18. /// 登录
  19. /// </summary>
  20. public class LoginController : BaseApiController
  21. {
  22. /// <summary>
  23. /// 获取验证码
  24. /// </summary>
  25. /// <returns></returns>
  26. [HttpGet, AllowAnonymous]
  27. public ActionResult GerVerifyCode()
  28. {
  29. var code = CaptchaCode.DrawNumberImage(4);
  30. HttpContext.Session.SetString(ProConst.VERIFY, code.Code);
  31. return File(code.ImgData, @"image/Gif");
  32. }
  33. /// <summary>
  34. /// 登录提交(带验证码)
  35. /// </summary>
  36. /// <returns></returns>
  37. [HttpPost, AllowAnonymous]
  38. public async Task<ActionResult<object>> AuthenticateVerify(LoginVerifyDto loginDto)
  39. {
  40. var code = HttpContext.Session.GetString(ProConst.VERIFY);
  41. if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(loginDto.Verify) || loginDto.Verify.ToLower() != code)
  42. throw new UseArgumentException("验证码不正确");
  43. return await Authenticate(loginDto.MapTo<LoginDto>());
  44. }
  45. /// <summary>
  46. /// 登录提交
  47. /// </summary>
  48. /// <returns></returns>
  49. [HttpPost, AllowAnonymous]
  50. public async Task<ActionResult<object>> Authenticate(LoginDto loginDto)
  51. {
  52. var url = ConfigHelper.Configuration["Login:Url"];
  53. Dictionary<string, string> dict = new Dictionary<string, string>();
  54. dict.Add("client_id", ConfigHelper.Configuration["Login:ClientId"]);
  55. dict.Add("client_secret", ConfigHelper.Configuration["Login:ClientSecrets"]);
  56. dict.Add("grant_type", "password");
  57. dict.Add("username", loginDto.Id);
  58. dict.Add("password", loginDto.Pwd);
  59. using(HttpClient http = new HttpClient())
  60. {
  61. using (var content = new FormUrlEncodedContent(dict))
  62. {
  63. var result = await http.PostAsync(url, content);
  64. if (result.IsSuccessStatusCode)
  65. {
  66. string msg = await result.Content.ReadAsStringAsync();
  67. return msg.ToModel<Result>();
  68. }
  69. else
  70. {
  71. return StatusCode((int)result.StatusCode);
  72. }
  73. }
  74. }
  75. }
  76. /// <summary>
  77. /// 单点登录
  78. /// </summary>
  79. /// <param name="ticket">票</param>
  80. /// <returns></returns>
  81. [HttpGet, AllowAnonymous]
  82. public async Task<ActionResult<object>> Single(string ticket)
  83. {
  84. var url = ConfigHelper.Configuration["Login:Url"];
  85. var url_ticket = ConfigHelper.Configuration["Auth:PT_Ticket"];
  86. try
  87. {
  88. using (HttpClient http = new HttpClient())
  89. {
  90. var result = await http.GetAsync(url_ticket + ticket);
  91. if (result.IsSuccessStatusCode)
  92. {
  93. string msg = await result.Content.ReadAsStringAsync();
  94. WS.Log.RunLog.Debug($"单点登录【{url_ticket + ticket}】:{msg}");
  95. var res = msg.ToModel<PtResult>();
  96. if (res != null && res.code == 200)
  97. {
  98. Dictionary<string, string> dict = new Dictionary<string, string>();
  99. dict.Add("client_id", ConfigHelper.Configuration["Login:ClientId"]);
  100. dict.Add("client_secret", ConfigHelper.Configuration["Login:ClientSecrets"]);
  101. dict.Add("grant_type", "password");
  102. dict.Add("username", res.data.userInfo.NO);
  103. dict.Add("password", "@AUTH@");
  104. using (var content = new FormUrlEncodedContent(dict))
  105. {
  106. result = await http.PostAsync(url, content);
  107. WS.Log.RunLog.Debug($"登录【{url}】:{result}");
  108. if (result.IsSuccessStatusCode)
  109. {
  110. msg = await result.Content.ReadAsStringAsync();
  111. return msg.ToModel<Result>();
  112. }
  113. else
  114. {
  115. return StatusCode((int)result.StatusCode);
  116. }
  117. }
  118. }
  119. else if (res == null)
  120. {
  121. return StatusCode(510, "单点登录接口返回错误");
  122. }
  123. else
  124. {
  125. return StatusCode(res.code);
  126. }
  127. }
  128. else
  129. {
  130. return StatusCode((int)result.StatusCode);
  131. }
  132. }
  133. }
  134. catch (System.Exception ex)
  135. {
  136. WS.Log.RunLog.Error(ex);
  137. throw new UseMassageException(ex.Message);
  138. }
  139. }
  140. /// <summary>
  141. /// 登录提交
  142. /// </summary>
  143. /// <returns></returns>
  144. [HttpPost, AllowAnonymous]
  145. public bool Logout()
  146. {
  147. //_iAuthManage.CancelAuth(HttpContext);
  148. return true;
  149. }
  150. }
  151. class Result
  152. {
  153. public string access_token { get; set; }
  154. public int expires_in { get; set; }
  155. public string token_type { get; set; }
  156. public string scope { get; set; }
  157. }
  158. }