BaseController.cs
2.96 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using HHECS.Application.Enums;
using HHECS.Dal.Repository;
using HHECS.Model.Entities;
using HHECS.Web.Aop;
using HHECS.WebCommon;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
namespace HHECS.Web.Controllers
{
/// <summary>
/// 基础控制器
/// <para>用于控制登录用户是否有权限访问指定的Action</para>
/// </summary>
[XSSFilter]
public class BaseController : Controller
{
protected string controllerName; //当前控制器小写名称
protected string actionName; //当前Action小写名称
protected HttpContext context { get; set; }
public const string token = "Token";
public const string referer = "Referer";
public const string origin = "Origin";
/// <summary>
/// 用户信息
/// </summary>
public User User { get; set; }
public BaseController()
{
}
/// <summary>
/// https://blog.csdn.net/mango_love/article/details/84992020
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
context = HttpContext;
var principal = HttpContext.AuthenticateAsync().Result.Principal;
var tokent = GetToken();
if (string.IsNullOrWhiteSpace(tokent))
{
filterContext.Result = new RedirectResult("/Login/Index");
}
User = new UserRepository().Where(t => t.Token == tokent).IncludeMany(t => t.Roles, then => then.IncludeMany(a => a.Permissions, then2 => then2.Include(a => a.Parent))).First();
if (User == null)
{
filterContext.Result = new RedirectResult("/Login/Index");
}
else if (principal == null)
{
filterContext.Result = new RedirectResult("/Login/Index");
}
else if (string.IsNullOrWhiteSpace(principal.Identity.Name))
{
filterContext.Result = new RedirectResult("/Login/Index");
}
}
public void ResponseEnumJosn()
{
//枚举输出到页面json
ViewBag.EnumIsVisible = typeof(EnumIsVisible).GetJsonEnum();
ViewBag.EnumIsDisable = typeof(EnumIsDisable).GetJsonEnum();
ViewBag.EnumPermissionType = EnumExtensions.GetEnumDescriptionList<EnumPermissionType>();
}
public string GetToken()
{
string tempToken = context.Request.Query[token];
if (!String.IsNullOrEmpty(tempToken)) return tempToken;
var cookie = context.Request.Cookies[token];
if (cookie == null)
{
cookie = context.Request.Headers["access-token"];
}
return cookie ?? String.Empty;
}
}
}