ibms-dome/Backend/ApiControllers/MyBaseApiController.cs

82 lines
3.6 KiB
C#
Raw Permalink Normal View History

2022-10-14 16:08:54 +08:00
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 Backend.Models;
using Backend.Jwt;
namespace Backend.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>();
//private IDatabaseHelper qqwt => HttpContext?.RequestServices.GetService<IDatabaseHelper>();
public MyBaseApiController() { }
protected JwtGet myUser;
protected string jwt_str = null;
protected bool jwtlife = true;
public string controllerName;
public string actionName;
//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();
var a = User.Claims.Select(p => new { Type = p.Type, Value = p.Value }).ToList();
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;
}
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);
}
}
}