ibms-dome/FrontendWebApi/ApiControllers/MyBaseApiController.cs

86 lines
3.9 KiB
C#
Raw Normal View History

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;
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;
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";
2022-10-14 16:08:54 +08:00
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(),
//};
2022-10-14 16:08:54 +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;
// }
//}
2022-10-14 16:08:54 +08:00
base.OnActionExecuting(filterContext);
}
}
}