public void OptionsPropertyShouldBeSetWhenPresentInTheEnvironment()
        {
            var owinContext = Repository.Create <IOwinContext>();
            var environment = new Dictionary <string, object>();
            var options     = new AuthorizationOptions();

            environment.Add(ResourceAuthorizationMiddleware.ServiceKey, options);
            owinContext.Setup(x => x.Environment).Returns(environment);
            var helper = new AuthorizationDependencyHelper(owinContext.Object);

            Assert.AreSame(options, helper.AuthorizationOptions);
        }
        private AuthorizationOptions ResolveAuthorizationOptions(IAuthorizationController controller)
        {
            if (controller != null)
            {
                return(controller.AuthorizationOptions);
            }

            var owinContext = _owinContextAccessor.Context;
            var helper      = new AuthorizationDependencyHelper(owinContext);

            return(helper.AuthorizationOptions);
        }
        public async Task <bool> IsAuthorizedAsync(IAuthorizationController controller, ClaimsPrincipal user, IAuthorizeData authorizeAttribute)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (authorizeAttribute == null)
            {
                throw new ArgumentNullException(nameof(authorizeAttribute));
            }

            AuthorizationOptions options;

            if (controller != null)
            {
                options = controller.AuthorizationOptions;
            }
            else
            {
                var owinContext = _owinContextAccessor.Context;
                var helper      = new AuthorizationDependencyHelper(owinContext);
                options = helper.AuthorizationOptions;
            }

            if (options == null)
            {
                throw new InvalidOperationException("AuthorizationOptions must not be null.  Your resource authorization may be set up incorrectly.");
            }

            if (options.Dependencies == null)
            {
                throw new InvalidOperationException("AuthorizationOptions.Dependencies must not be null");
            }

            var policyProvider       = options.Dependencies.PolicyProvider ?? new DefaultAuthorizationPolicyProvider(options);
            var authorizationService = GetAuthorizationService(options, policyProvider);
            var policy = await AuthorizationPolicy.CombineAsync(policyProvider, new[] { authorizeAttribute });

            return(await authorizationService.AuthorizeAsync(user, policy));
        }