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
using Bootstrap.Security.Mvc;
using Microsoft.AspNetCore.Builder;
using System.Linq;
using System.Threading.Tasks;
 
namespace Microsoft.AspNetCore.Authorization
{
    /// <summary>
    /// BootstrapAdmin 应用程序授权要求类
    /// </summary>
    internal class AppRequirement : AuthorizationHandler<AppRequirement>, IAuthorizationRequirement
    {
        /// <summary>
        /// 处理授权要求方法
        /// </summary>
        /// <param name="context"></param>
        /// <param name="requirement"></param>
        /// <returns></returns>
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AppRequirement requirement)
        {
            if (context.User.Identity.IsAuthenticated)
            {
                // 判断应用程序授权
                var appId = BootstrapAppContext.AppId;
                if (string.IsNullOrEmpty(appId)) context.Succeed(this);
                else if (context.User.IsInRole("Administrators")) context.Succeed(this);
                else if (!string.IsNullOrEmpty(context.User.Identity.Name) && AuthenticationExtensions.RetrieveAppsByUserName(context.User.Identity.Name).Any(a => a == appId)) context.Succeed(this);
            }
            return Task.CompletedTask;
        }
    }
}