zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
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
using Bootstrap.Security.Authorization;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
 
namespace Microsoft.AspNetCore.Mvc.Filters
{
    /// <summary>
    /// 按钮权限检查过滤器
    /// </summary>
    public class ButtonAuthorizeAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 按钮授权所属菜单
        /// </summary>
        public string Url { get; set; } = "";
 
        /// <summary>
        /// 按钮授权标识码
        /// </summary>
        public string Auth { get; set; } = "";
 
        /// <summary>
        /// 控制器执行异步调用方法
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var buttonAuthorizateSvr = context.HttpContext.RequestServices.GetRequiredService<IButtonAuthorization>();
            if (buttonAuthorizateSvr == null || !buttonAuthorizateSvr.Authorizate(context.HttpContext.User, Url, Auth))
            {
                context.Result = new UnauthorizedResult();
            }
            return base.OnActionExecutionAsync(context, next);
        }
    }
}