BaseController.cs
2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Linq;
using System.Reflection;
using WebApp;
namespace WebMvc
{
/// <summary>
/// 基础控制器
/// <para>用于控制登录用户是否有权限访问指定的Action</para>
/// </summary>
public class BaseController : SSOController
{
protected Response Result = new Response();
protected string Controllername; //当前控制器小写名称
protected string Actionname; //当前Action小写名称
public BaseController(IAuth authUtil) : base(authUtil)
{
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
var description =
(Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)filterContext.ActionDescriptor;
Controllername = description.ControllerName.ToLower();
Actionname = description.ActionName.ToLower();
var function = ((TypeInfo)GetType()).DeclaredMethods.FirstOrDefault(u => u.Name.ToLower() == Actionname);
if (function == null)
throw new Exception("未能找到Action");
//权限验证标识
var authorize = function.GetCustomAttribute(typeof(AuthenticateAttribute));
if (authorize == null)
{
return;
}
AuthStrategyContext authStrategyContext = _authUtil.GetCurrentUser();
if (authStrategyContext == null)
{
filterContext.Result = new RedirectResult("/Login/Index");
return;
}
var currentModule = authStrategyContext.Modules.FirstOrDefault(u => u.Url.ToLower().Contains(Controllername));
//当前登录用户没有Action记录&&Action有authenticate标识
if (currentModule == null)
{
filterContext.Result = new RedirectResult("/Login/Index");
return;
}
}
//监控掉线
[HttpPost]
public string Ping()
{
return JsonHelper.Instance.Serialize(Result);
}
}
}