107 lines
3.4 KiB
C#
107 lines
3.4 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.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;
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
[Route("sync")]
|
|
public async Task<MessageModel<bool>> DataSync([FromBody] object obj)
|
|
{
|
|
string newobj = obj.ToString().Replace("object", "_object");
|
|
// CasdoorHookModel
|
|
|
|
|
|
return new MessageModel<bool>()
|
|
{
|
|
|
|
};
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///Oauth2验证回调
|
|
/// </summary>
|
|
/// <param name="code"></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", 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.GetUserInfo(new Model.Request.LoginQuery() { openId = userinfo.sub });
|
|
if (user != null)
|
|
{
|
|
if (user.Item1)
|
|
{
|
|
TokenModelJwt tokenModel = new TokenModelJwt { Uid = user.Item2.Id, Role = "users" };
|
|
jwtStr = JwtHelper.IssueJwt(tokenModel);
|
|
response.token = jwtStr;
|
|
suc = true;
|
|
}
|
|
else
|
|
{
|
|
return new MessageModel<CasDoorToken>()
|
|
{
|
|
success = false
|
|
|
|
};
|
|
}
|
|
}
|
|
return new MessageModel<CasDoorToken>()
|
|
{
|
|
success = suc,
|
|
msg = suc ? "success" : "fail",
|
|
response = response
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|