40 lines
1.6 KiB
C#
40 lines
1.6 KiB
C#
using Common;
|
|||
|
|
using CommonInfrastructure.Response;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||
|
|
|
||
|
|
namespace CommonInfrastructure
|
||
|
|
{
|
||
|
|
public class BaseController : Controller
|
||
|
|
{
|
||
|
|
public LoginUser loginUser { get; set; }
|
||
|
|
|
||
|
|
public override void OnActionExecuting(ActionExecutingContext filterContext)
|
||
|
|
{
|
||
|
|
var context = filterContext.HttpContext;
|
||
|
|
|
||
|
|
loginUser = new LoginUser();
|
||
|
|
var request = context.Request;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
loginUser.UserId = context.User.Claims.FirstOrDefault(e => e.Type == "uid")?.Value;
|
||
|
|
loginUser.UserName = context.User.Claims.FirstOrDefault(e => e.Type == "uname")?.Value;
|
||
|
|
loginUser.DisplayName = context.User.Claims.FirstOrDefault(e => e.Type == "dname")?.Value;
|
||
|
|
loginUser.Icon = context.User.Claims.FirstOrDefault(e => e.Type == "avatar")?.Value;
|
||
|
|
loginUser.Number = context.User.Claims.FirstOrDefault(e => e.Type == "number")?.Value;
|
||
|
|
|
||
|
|
loginUser.Ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault()?.Split(',').FirstOrDefault()?.Trim();
|
||
|
|
|
||
|
|
if (string.IsNullOrEmpty(loginUser.Ip))
|
||
|
|
{
|
||
|
|
loginUser.Ip = context.Connection.RemoteIpAddress?.ToString() ?? "";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
filterContext.Result = new JsonResult(new BaseCommonResponse() { Code = CommonResponseCodeEnum.ParamError, Info = "请求参数异常"+ex.Message });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|