コード例 #1
0
        public async Task <IActionResult> OnGetAsync()
        {
            var authResult = await _auth.AuthorizeAsync(User,
                                                        ArticlePolicies.ListArticles2);

            if (!authResult.Succeeded)
            {
                ArticlesHelper.LogFailure(_logger, authResult.Failure);
                return(new ChallengeResult());
            }

            var userMaturity = MaturityHelper.GetMaturity(User);
            var userName     = User.Identity.Name;

            Article = await _context.Articles
                      .EnforceAgeAndOwner(userName, userMaturity)
                      .ToListAsync();

            return(Page());

            //var denied = res
            //    .Where(r => !r.AuthResultTask.Result.Succeeded)
            //    .Select(r => new FailureDescriptor()
            //    {
            //        Article = r.Article,
            //        Failure = r.AuthResultTask.Result.Failure,
            //    })
            //    .ToList();

            //ArticlesHelper.LogFailure(_logger, denied);
        }
コード例 #2
0
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
                                                       AgeRequirement requirement, Article resource)
        {
            var userMaturity = MaturityHelper.GetMaturity(context.User);

            if (userMaturity >= resource.Maturity)
            {
                context.Succeed(requirement);
            }

            // not allowed because it requires
            // more seniority
            return(Task.CompletedTask);
        }