mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-20 10:37:55 +08:00
转移.net core 3.1,为.NET 5做准备
This commit is contained in:
63
OpenAuth.WebApi/Model/OpenAuthFilter.cs
Normal file
63
OpenAuth.WebApi/Model/OpenAuthFilter.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System.Reflection;
|
||||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using OpenAuth.App;
|
||||
using OpenAuth.App.Interface;
|
||||
using OpenAuth.Repository.Domain;
|
||||
|
||||
namespace OpenAuth.WebApi.Model
|
||||
{
|
||||
public class OpenAuthFilter : IActionFilter
|
||||
{
|
||||
private readonly IAuth _authUtil;
|
||||
private readonly SysLogApp _logApp;
|
||||
|
||||
public OpenAuthFilter(IAuth authUtil, SysLogApp logApp)
|
||||
{
|
||||
_authUtil = authUtil;
|
||||
_logApp = logApp;
|
||||
}
|
||||
|
||||
public void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
var description =
|
||||
(Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor;
|
||||
|
||||
var Controllername = description.ControllerName.ToLower();
|
||||
var Actionname = description.ActionName.ToLower();
|
||||
|
||||
//匿名标识
|
||||
var authorize = description.MethodInfo.GetCustomAttribute(typeof(AllowAnonymousAttribute));
|
||||
if (authorize != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_authUtil.CheckLogin())
|
||||
{
|
||||
context.HttpContext.Response.StatusCode = 401;
|
||||
context.Result = new JsonResult(new Response
|
||||
{
|
||||
Code = 401,
|
||||
Message = "认证失败,请提供认证信息"
|
||||
});
|
||||
return;
|
||||
}
|
||||
_logApp.Add(new SysLog
|
||||
{
|
||||
Content = $"用户访问",
|
||||
Href = $"{Controllername}/{Actionname}",
|
||||
CreateName = _authUtil.GetUserName(),
|
||||
CreateId = _authUtil.GetCurrentUser().User.Id,
|
||||
TypeName = "访问日志"
|
||||
});
|
||||
}
|
||||
|
||||
public void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user