ibms-dome/FrontendWebApi/ApiControllers/MyBaseApiController.cs

86 lines
3.9 KiB
C#

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;
using System.IdentityModel.Tokens.Jwt;
using System.Net;
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>();
public string baseURL => HttpContext?.Request.Scheme + "://" + HttpContext?.Request.Host + "/";
public MyBaseApiController() { }
protected JwtGet myUser;
protected string jwt_str = null;
protected bool jwtlife = true;
public string controllerName;
public string actionName;
public string main_system_type = "device_system_category_layer2";
public string sub_system_type = "device_system_category_layer3";
public string forge_node_name_system_type = "forge_check_tag_id";
public ErrorCode errorCode = new ErrorCode();
[Authorize]
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//controllerName = ControllerContext.RouteData.Values["controller"].ToString(); //controller名稱
//actionName = ControllerContext.RouteData.Values["action"].ToString(); //action名稱
//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();
//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(),
//};
//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;
// }
//}
base.OnActionExecuting(filterContext);
}
}
}