147 lines
4.9 KiB
C#
147 lines
4.9 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using New_College.AuthHelper.OverWrite;
|
|
using New_College.Common;
|
|
using New_College.Common.Helper;
|
|
using New_College.IServices;
|
|
using New_College.Model;
|
|
using New_College.Model.ViewModels;
|
|
using NPOI.SS.Formula.Functions;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace New_College.Controllers
|
|
{
|
|
|
|
[AllowAnonymous]
|
|
[Route("api/oauth")]
|
|
public class OauthController : Controller
|
|
{
|
|
readonly IV_CustomerInfoServices _CustomerInfoServices;
|
|
public OauthController(IV_CustomerInfoServices v_CustomerInfoServices)
|
|
{
|
|
_CustomerInfoServices = v_CustomerInfoServices;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
///三方登录
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
//[Route("login")]
|
|
//public async Task<MessageModel<string>> Login([FromQuery] ThridLoginRequestDto dto)
|
|
//{
|
|
// string token = string.Empty;
|
|
// if (dto == null)
|
|
// {
|
|
// return new MessageModel<string>()
|
|
// {
|
|
// success = false,
|
|
// msg = "参数错误",
|
|
|
|
// };
|
|
// }
|
|
// if (ThridConfig.secretKey != dto.secretKey)
|
|
// {
|
|
// return new MessageModel<string>()
|
|
// {
|
|
// success = false,
|
|
// msg = "密钥错误",
|
|
// };
|
|
// }
|
|
// //首先判断是否存在账户|
|
|
// //不存在则创建新的账户|
|
|
// //存在则直接登录|
|
|
|
|
|
|
|
|
// return new MessageModel<string>()
|
|
// {
|
|
// msg = "ok",
|
|
// success = true,
|
|
// response = token
|
|
// };
|
|
//}
|
|
|
|
/// <summary>
|
|
/// Oauth2验证回调
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <param name="state"></param>
|
|
/// <param name="scope"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Route("callback")]
|
|
public async Task<MessageModel<CasDoorToken>> Callback(string code, string state, string scope)
|
|
{
|
|
var response = new CasDoorToken();
|
|
string jwtStr = string.Empty;
|
|
bool suc = false;
|
|
var getaccesstoken = CasdoorHttpHelper.Http_Post<CasdoorGetTokenResponse>("api/login/oauth/access_token", null, new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new CasdoorRequest() { code = code, grant_type = "authorization_code", client_id = CasdoorConfig.ClientId, client_secret = CasdoorConfig.ClientSecret }), Encoding.UTF8, "application/json"));//获取access_token
|
|
var headers = new System.Collections.Generic.Dictionary<string, string>
|
|
{
|
|
{ "Authorization", string.Format("Bearer {0}", getaccesstoken.access_token) }
|
|
};
|
|
if (getaccesstoken.access_token == null)
|
|
{
|
|
return new MessageModel<CasDoorToken>()
|
|
{
|
|
msg = "code已失效",
|
|
success = false
|
|
};
|
|
}
|
|
response.servicetoken = getaccesstoken.access_token;
|
|
var userinfo = CasdoorHttpHelper.Http_Get<CasdoorUserInfoDto>("/api/userinfo", headers, new Dictionary<string, string>());
|
|
var user = (await _CustomerInfoServices.Query(q => q.UUID == userinfo.sub)).FirstOrDefault();
|
|
if (user != null)
|
|
{
|
|
if (string.IsNullOrEmpty(user.NickName))
|
|
{
|
|
var single = await _CustomerInfoServices.QueryById(user.Id);
|
|
if (!string.IsNullOrWhiteSpace(user.Phone))
|
|
{
|
|
single.NickName = user.Phone;
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(user.UserCode))
|
|
{
|
|
single.NickName = user.UserCode;
|
|
}
|
|
await _CustomerInfoServices.Update(single);
|
|
}
|
|
TokenModelJwt tokenModel = new TokenModelJwt { Uid = user.Id, Role = "users" };
|
|
jwtStr = JwtHelper.IssueJwt(tokenModel);
|
|
response.token = jwtStr;
|
|
response.Id = user.Id;
|
|
suc = true;
|
|
|
|
}
|
|
else
|
|
{
|
|
return new MessageModel<CasDoorToken>()
|
|
{
|
|
success = false,
|
|
msg = "该账户不存在或已注销"
|
|
|
|
};
|
|
}
|
|
return new MessageModel<CasDoorToken>()
|
|
{
|
|
success = suc,
|
|
msg = suc ? "success" : "fail",
|
|
response = response
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|