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
{
///
/// 登录
///
public class LoginController : BaseApiController
{
///
/// 获取验证码
///
///
[HttpGet, AllowAnonymous]
public ActionResult GerVerifyCode()
{
var code = CaptchaCode.DrawNumberImage(4);
HttpContext.Session.SetString(ProConst.VERIFY, code.Code);
return File(code.ImgData, @"image/Gif");
}
///
/// 登录提交(带验证码)
///
///
[HttpPost, AllowAnonymous]
public async Task> 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());
}
///
/// 登录提交
///
///
[HttpPost, AllowAnonymous]
public async Task> Authenticate(LoginDto loginDto)
{
var url = ConfigHelper.Configuration["Login:Url"];
Dictionary dict = new Dictionary();
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();
}
else
{
return StatusCode((int)result.StatusCode);
}
}
}
}
///
/// 单点登录
///
/// 票
///
[HttpGet, AllowAnonymous]
public async Task> 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();
if (res != null && res.code == 200)
{
Dictionary dict = new Dictionary();
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();
}
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);
}
}
///
/// 登录提交
///
///
[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; }
}
}