2022-10-14 16:08:54 +08:00
|
|
|
|
using FrontendWebApi.Jwt;
|
|
|
|
|
using FrontendWebApi.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Repository.BackendRepository.Interface;
|
|
|
|
|
using Repository.BaseRepository.Interface;
|
|
|
|
|
using Repository.FrontendRepository.Interface;
|
|
|
|
|
using Repository.Helper;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-11-04 09:41:50 +08:00
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
using System.Net;
|
2022-10-14 16:08:54 +08:00
|
|
|
|
|
|
|
|
|
namespace FrontendWebApi.ApiControllers
|
|
|
|
|
{
|
|
|
|
|
public class MyBaseApiController<T> : Controller where T : MyBaseApiController<T>
|
|
|
|
|
{
|
|
|
|
|
private ILogger<T> _logger;
|
|
|
|
|
protected ILogger<T> Logger => _logger ?? (_logger = HttpContext?.RequestServices.GetService<ILogger<T>>());
|
|
|
|
|
private IJwtHelpers jwt => HttpContext?.RequestServices.GetService<IJwtHelpers>();
|
2022-11-17 18:25:40 +08:00
|
|
|
|
public string baseURL => HttpContext?.Request.Scheme + "://" + HttpContext?.Request.Host + "/";
|
2022-10-14 16:08:54 +08:00
|
|
|
|
|
|
|
|
|
public MyBaseApiController() { }
|
|
|
|
|
protected JwtGet myUser;
|
|
|
|
|
protected string jwt_str = null;
|
|
|
|
|
protected bool jwtlife = true;
|
|
|
|
|
public string controllerName;
|
|
|
|
|
public string actionName;
|
2022-11-01 10:49:22 +08:00
|
|
|
|
public string main_system_type = "device_system_category_layer2";
|
|
|
|
|
public string sub_system_type = "device_system_category_layer3";
|
2022-12-31 11:13:35 +08:00
|
|
|
|
public string forge_node_name_system_type = "forge_check_tag_id";
|
2022-10-14 16:08:54 +08:00
|
|
|
|
public ErrorCode errorCode = new ErrorCode();
|
|
|
|
|
[Authorize]
|
|
|
|
|
public override void OnActionExecuting(ActionExecutingContext filterContext)
|
|
|
|
|
{
|
2023-01-06 10:16:40 +08:00
|
|
|
|
controllerName = ControllerContext.RouteData.Values["controller"].ToString(); //controller名稱
|
|
|
|
|
actionName = ControllerContext.RouteData.Values["action"].ToString(); //action名稱
|
2022-12-31 11:13:35 +08:00
|
|
|
|
|
2023-01-06 10:16:40 +08:00
|
|
|
|
var ctx = filterContext.HttpContext;
|
|
|
|
|
ctx.Response.Headers.Add("Access-Control-Allow-Origin", "*");
|
|
|
|
|
ctx.Response.Headers.Add("Access-Control-Allow-Headers", "*");
|
|
|
|
|
ctx.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
|
|
|
|
|
EDFunction edFunction = new EDFunction();
|
2023-05-10 15:14:46 +08:00
|
|
|
|
//myUser = new JwtGet()
|
|
|
|
|
//{
|
|
|
|
|
// account = User.Claims.Where(a => a.Type == "account").Select(e => e.Value).FirstOrDefault(),
|
|
|
|
|
// email = User.Claims.Where(a => a.Type == "email").Select(e => e.Value).FirstOrDefault(),
|
|
|
|
|
// full_name = User.Claims.Where(a => a.Type == "full_name").Select(e => e.Value).FirstOrDefault(),
|
|
|
|
|
// exp = User.Claims.Where(a => a.Type == "exp").Select(e => Convert.ToInt32(e.Value)).FirstOrDefault(),
|
|
|
|
|
// nbf = User.Claims.Where(a => a.Type == "nbf").Select(e => Convert.ToInt32(e.Value)).FirstOrDefault(),
|
|
|
|
|
// userinfo_guid = User.Claims.Where(a => a.Type == "userinfo_guid").Select(e => e.Value).FirstOrDefault(),
|
|
|
|
|
//};
|
2022-10-14 16:08:54 +08:00
|
|
|
|
|
2023-05-10 15:14:46 +08:00
|
|
|
|
//if (myUser.exp == 0)
|
|
|
|
|
//{
|
|
|
|
|
// jwt_str = "Jwt Token不合法";
|
|
|
|
|
// jwtlife = false;
|
|
|
|
|
// filterContext.Result = new JsonResult(new { HttpStatusCode.Unauthorized });
|
|
|
|
|
//}
|
|
|
|
|
//else
|
|
|
|
|
//{
|
|
|
|
|
// if (myUser.exp <= DateTime.Now.AddHours(-8).AddMinutes(10).Subtract(new DateTime(1970, 1, 1)).TotalSeconds)
|
|
|
|
|
// {
|
|
|
|
|
// jwtlife = true;
|
|
|
|
|
// JwtLogin jwtLoing = new JwtLogin()
|
|
|
|
|
// {
|
|
|
|
|
// account = myUser.account,
|
|
|
|
|
// email = myUser.email,
|
|
|
|
|
// full_name = myUser.full_name,
|
|
|
|
|
// userinfo_guid = myUser.userinfo_guid
|
|
|
|
|
// };
|
|
|
|
|
// jwt_str = jwt.GenerateToken(jwtLoing).token;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2023-02-06 10:22:48 +08:00
|
|
|
|
//if (myUser.exp <= DateTime.Now.AddHours(-8).AddMinutes(10).Subtract(new DateTime(1970, 1, 1)).TotalSeconds)
|
|
|
|
|
//{
|
|
|
|
|
// jwtlife = true;
|
|
|
|
|
// JwtLogin jwtLoing = new JwtLogin()
|
|
|
|
|
// {
|
|
|
|
|
// account ="webUser",
|
|
|
|
|
// email = "webUser@gmail.com",
|
|
|
|
|
// full_name = "webUser",
|
|
|
|
|
// userinfo_guid = "6ac24708-3a40-4199-88c5-22df310cd1a8"
|
|
|
|
|
// };
|
|
|
|
|
// jwt_str = jwt.GenerateToken(jwtLoing).token;
|
|
|
|
|
//}
|
2022-10-14 16:08:54 +08:00
|
|
|
|
base.OnActionExecuting(filterContext);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|