80 lines
3.5 KiB
C#
80 lines
3.5 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;
|
|||
|
|
|||
|
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 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);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|