Тестирование AuthorHandlers с HttpContext

Я тестирую свои собственные AuthorizationHandlers, но когда я получаю HttpContext, он становится пустым.

        private IAuthorizationService BuildAuthorizationService(Action<IServiceCollection> setupServices = null)
        {
            var services = new ServiceCollection();
            services.AddAuthorization();
            services.AddOptions();
            services.AddLogging();

            setupServices?.Invoke(services);
            return services.BuildServiceProvider().GetRequiredService<IAuthorizationService>();
        }
        [Fact]
        public async Task ShouldAllowIfScopeClaimWorkflowAdminIsPresent()
        {
            // Arrange
            var authorizationService = BuildAuthorizationService(services =>
            {
                services.AddSingleton<IAuthorizationHandler, Jarvis.Security.Handlers.ModulePermissionHandlers>();
                services.AddAuthorization(options =>
                {
                    options.AddPolicy("SomePolicyName",
                        policy => policy.Requirements.Add(new Jarvis.Security.Handlers.PermissionRequirement()));
                });
            });
            var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim("Issuer", "WebApplication1") }));

            // Act
            var allowed = await authorizationService.AuthorizeAsync(user, "SomePolicyName");

            // Assert
            Assert.True(allowed.Succeeded);
        }
    }
\\\Hanlder
 protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
        {
            var ctx = (AuthorizationFilterContext)context.Resource;
            var intention = ctx.HttpContext.Request.Method;
            var resource = ctx.RouteData.Values["controller"].ToString();

            if (context.User.HasClaim(c => c.Issuer == resource))
                context.Succeed(requirement);

            return Task.CompletedTask;
        }

Когда я хочу получить var намерение = ctx.HttpContext.Request.Method; это ноль:/ Любое предложение

0 ответов

Другие вопросы по тегам