| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using QM.AuthServer;
- using QM.AuthServer.Auth;
- using QM.AuthServer.Models.Users;
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Threading.Tasks;
- using WS;
- using WS.AutoMapper;
- using WS.Helper;
- using WS.Image;
- using WS.WebCore.Api;
- namespace QM.Gateway.Controllers
- {
- /// <summary>
- /// 登录
- /// </summary>
- public class LoginController : BaseApiController
- {
- /// <summary>
- /// 获取验证码
- /// </summary>
- /// <returns></returns>
- [HttpGet, AllowAnonymous]
- public ActionResult GerVerifyCode()
- {
- var code = CaptchaCode.DrawNumberImage(4);
- HttpContext.Session.SetString(ProConst.VERIFY, code.Code);
- return File(code.ImgData, @"image/Gif");
- }
- /// <summary>
- /// 登录提交(带验证码)
- /// </summary>
- /// <returns></returns>
- [HttpPost, AllowAnonymous]
- public async Task<ActionResult<object>> AuthenticateVerify(LoginVerifyDto loginDto)
- {
- var code = HttpContext.Session.GetString(ProConst.VERIFY);
- if (string.IsNullOrWhiteSpace(code) || string.IsNullOrWhiteSpace(loginDto.Verify) || loginDto.Verify.ToLower() != code)
- throw new UseArgumentException("验证码不正确");
- return await Authenticate(loginDto.MapTo<LoginDto>());
- }
- /// <summary>
- /// 登录提交
- /// </summary>
- /// <returns></returns>
- [HttpPost, AllowAnonymous]
- public async Task<ActionResult<object>> Authenticate(LoginDto loginDto)
- {
- var url = ConfigHelper.Configuration["Login:Url"];
- Dictionary<string, string> dict = new Dictionary<string, string>();
- dict.Add("client_id", ConfigHelper.Configuration["Login:ClientId"]);
- dict.Add("client_secret", ConfigHelper.Configuration["Login:ClientSecrets"]);
- dict.Add("grant_type", "password");
- dict.Add("username", loginDto.Id);
- dict.Add("password", loginDto.Pwd);
- using(HttpClient http = new HttpClient())
- {
- using (var content = new FormUrlEncodedContent(dict))
- {
- var result = await http.PostAsync(url, content);
- if (result.IsSuccessStatusCode)
- {
- string msg = await result.Content.ReadAsStringAsync();
- return msg.ToModel<Result>();
- }
- else
- {
- return StatusCode((int)result.StatusCode);
- }
- }
- }
- }
-
- /// <summary>
- /// 单点登录
- /// </summary>
- /// <param name="ticket">票</param>
- /// <returns></returns>
- [HttpGet, AllowAnonymous]
- public async Task<ActionResult<object>> Single(string ticket)
- {
- var url = ConfigHelper.Configuration["Login:Url"];
- var url_ticket = ConfigHelper.Configuration["Auth:PT_Ticket"];
- try
- {
- using (HttpClient http = new HttpClient())
- {
- var result = await http.GetAsync(url_ticket + ticket);
- if (result.IsSuccessStatusCode)
- {
- string msg = await result.Content.ReadAsStringAsync();
- WS.Log.RunLog.Debug($"单点登录【{url_ticket + ticket}】:{msg}");
- var res = msg.ToModel<PtResult>();
- if (res != null && res.code == 200)
- {
- Dictionary<string, string> dict = new Dictionary<string, string>();
- dict.Add("client_id", ConfigHelper.Configuration["Login:ClientId"]);
- dict.Add("client_secret", ConfigHelper.Configuration["Login:ClientSecrets"]);
- dict.Add("grant_type", "password");
- dict.Add("username", res.data.userInfo.NO);
- dict.Add("password", "@AUTH@");
- using (var content = new FormUrlEncodedContent(dict))
- {
- result = await http.PostAsync(url, content);
- WS.Log.RunLog.Debug($"登录【{url}】:{result}");
- if (result.IsSuccessStatusCode)
- {
- msg = await result.Content.ReadAsStringAsync();
- return msg.ToModel<Result>();
- }
- else
- {
- return StatusCode((int)result.StatusCode);
- }
- }
- }
- else if (res == null)
- {
- return StatusCode(510, "单点登录接口返回错误");
- }
- else
- {
- return StatusCode(res.code);
- }
- }
- else
- {
- return StatusCode((int)result.StatusCode);
- }
- }
- }
- catch (System.Exception ex)
- {
- WS.Log.RunLog.Error(ex);
- throw new UseMassageException(ex.Message);
- }
- }
- /// <summary>
- /// 登录提交
- /// </summary>
- /// <returns></returns>
- [HttpPost, AllowAnonymous]
- public bool Logout()
- {
- //_iAuthManage.CancelAuth(HttpContext);
- return true;
- }
- }
- class Result
- {
- public string access_token { get; set; }
- public int expires_in { get; set; }
- public string token_type { get; set; }
- public string scope { get; set; }
- }
- }
|