当前位置: 首页>>代码示例>>C#>>正文


C# PolicyContainer.AddPolicy方法代码示例

本文整理汇总了C#中PolicyContainer.AddPolicy方法的典型用法代码示例。如果您正苦于以下问题:C# PolicyContainer.AddPolicy方法的具体用法?C# PolicyContainer.AddPolicy怎么用?C# PolicyContainer.AddPolicy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PolicyContainer的用法示例。


在下文中一共展示了PolicyContainer.AddPolicy方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetUp

        public void SetUp()
        {
            // Arrange
            _policyContainer = TestDataFactory.CreateValidPolicyContainer();

            // Act
            _return = _policyContainer.AddPolicy<SomePolicy>();
        }
开发者ID:pvivera,项目名称:FluentSecurity,代码行数:8,代码来源:PolicyContainerSpec.cs

示例2: Should_enforce_policies_with_context

        public void Should_enforce_policies_with_context()
        {
            // Arrange
            var roles = new List<object> { UserRole.Owner }.ToArray();
            const bool isAuthenticated = true;

            var context = new Mock<ISecurityContext>();
            context.Setup(x => x.CurrenUserAuthenticated()).Returns(isAuthenticated);
            context.Setup(x => x.CurrenUserRoles()).Returns(roles);

            var policy = new Mock<ISecurityPolicy>();
            policy.Setup(x => x.Enforce(It.Is<ISecurityContext>(c => c.CurrenUserAuthenticated() == isAuthenticated && c.CurrenUserRoles() == roles))).Returns(PolicyResult.CreateSuccessResult(policy.Object));

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(policy.Object);

            // Act
            policyContainer.EnforcePolicies(context.Object);

            // Assert
            policy.VerifyAll();
        }
开发者ID:dsekar,项目名称:FluentSecurity,代码行数:22,代码来源:PolicyContainerSpec.cs

示例3: Should_return_the_same_results

        public void Should_return_the_same_results()
        {
            // Arrange
            var context = TestDataFactory.CreateSecurityContext(false);
            context.Runtime.As<SecurityRuntime>().DefaultResultsCacheLifecycle = Cache.PerHttpSession;
            var firstPolicy = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(firstPolicy);

            // Act
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            SecurityCache.ClearCache(Lifecycle.HybridHttpSession); ;

            var results3 = policyContainer.EnforcePolicies(context);
            var results4 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
            Assert.That(results3.Single(), Is.EqualTo(results4.Single()));

            Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across sessions.");
        }
开发者ID:pvivera,项目名称:FluentSecurity,代码行数:24,代码来源:PolicyContainerSpec.cs

示例4: Should_return_unique_results

        public void Should_return_unique_results()
        {
            // Arrange
            var context = TestDataFactory.CreateSecurityContext(false);
            context.Runtime.As<SecurityRuntime>().DefaultResultsCacheLifecycle = Cache.DoNotCache;
            var firstPolicy = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(firstPolicy);

            // Act

            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.Not.EqualTo(results2.Single()));
        }
开发者ID:pvivera,项目名称:FluentSecurity,代码行数:17,代码来源:PolicyContainerSpec.cs

示例5: Should_use_cache_lifecycle_specified_when_adding_a_policy

        public void Should_use_cache_lifecycle_specified_when_adding_a_policy()
        {
            // Arrange
            const Cache defaultCacheLifecycle = Cache.PerHttpSession;
            const Cache specifiedCacheLifecycle = Cache.PerHttpRequest;

            var context = TestDataFactory.CreateSecurityContext(false);
            context.Runtime.As<SecurityRuntime>().DefaultResultsCacheLifecycle = defaultCacheLifecycle;
            var securityPolicy = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(securityPolicy).Cache<IgnorePolicy>(specifiedCacheLifecycle);

            // Act
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            SecurityCache.ClearCache(Lifecycle.HybridHttpContext); ;

            var results3 = policyContainer.EnforcePolicies(context);
            var results4 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
            Assert.That(results3.Single(), Is.EqualTo(results4.Single()));

            Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across requests.");
        }
开发者ID:pvivera,项目名称:FluentSecurity,代码行数:27,代码来源:PolicyContainerSpec.cs

示例6: Should_stop_on_first_violation_and_return_2_results

        public void Should_stop_on_first_violation_and_return_2_results()
        {
            // Arrange
            var context = TestDataFactory.CreateSecurityContext(false);

            var firstPolicy = new TestPolicy();

            var secondPolicy = new Mock<ISecurityPolicy>();
            secondPolicy.Setup(x => x.Enforce(It.IsAny<ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(secondPolicy.Object, "Failure occured"));

            var thirdPolicy = new TestPolicy();

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(firstPolicy).AddPolicy(secondPolicy.Object).AddPolicy(thirdPolicy);

            // Act
            var results = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results.Count(), Is.EqualTo(2));
            Assert.That(results.First().ViolationOccured, Is.False);
            Assert.That(results.Last().ViolationOccured, Is.True);
        }
开发者ID:pvivera,项目名称:FluentSecurity,代码行数:23,代码来源:PolicyContainerSpec.cs

示例7: Should_return_results

        public void Should_return_results()
        {
            // Arrange
            var roles = new List<object> { UserRole.Owner }.ToArray();
            const bool isAuthenticated = true;
            const string failureOccured = "Failure occured";
            var context = TestDataFactory.CreateSecurityContext(isAuthenticated, roles);

            var policy = new Mock<ISecurityPolicy>();
            policy.Setup(x => x.Enforce(It.IsAny<ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(policy.Object, failureOccured));

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(policy.Object);

            // Act
            var results = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results.Count(), Is.EqualTo(1));
            Assert.That(results.Single().ViolationOccured, Is.True);
            Assert.That(results.Single().Message, Is.EqualTo(failureOccured));
        }
开发者ID:pvivera,项目名称:FluentSecurity,代码行数:22,代码来源:PolicyContainerSpec.cs

示例8: Should_invoke_the_isautheticated_and_roles_functions

        public void Should_invoke_the_isautheticated_and_roles_functions()
        {
            // Arrange
            var context = MockRepository.GenerateMock<ISecurityContext>();
            context.Expect(x => x.Runtime).Return(TestDataFactory.CreateSecurityRuntime()).Repeat.Once();
            context.Expect(x => x.CurrentUserIsAuthenticated()).Return(true).Repeat.Once();
            context.Expect(x => x.CurrentUserRoles()).Return(new List<object> { UserRole.Owner }.ToArray()).Repeat.Once();
            context.Replay();

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(new TestPolicy());

            // Act
            policyContainer.EnforcePolicies(context);

            // Assert
            context.VerifyAllExpectations();
        }
开发者ID:pvivera,项目名称:FluentSecurity,代码行数:18,代码来源:PolicyContainerSpec.cs

示例9: Should_load_lazy_policy_with_cache_key_exactly_twice_during_execution_with_caching_on

        public void Should_load_lazy_policy_with_cache_key_exactly_twice_during_execution_with_caching_on()
        {
            // Arrange
            var callsToContainer = 0;
            var policy = new LazyLoadedPolicyWithCacheKey();
            FakeIoC.GetAllInstancesProvider = () =>
            {
                callsToContainer++;
                return new List<object> { policy };
            };
            SecurityConfigurator.Configure(configuration =>
            {
                configuration.GetAuthenticationStatusFrom(TestDataFactory.ValidIsAuthenticatedFunction);
                configuration.ResolveServicesUsing(FakeIoC.GetAllInstances);
                configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.PerHttpRequest);
            });
            var context = new MockSecurityContext(runtime: SecurityConfiguration.Current.Runtime);
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy<LazyLoadedPolicyWithCacheKey>();

            // Act
            policyContainer.EnforcePolicies(context);
            policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(callsToContainer, Is.EqualTo(2));
            Assert.That(policy.CacheKeyCallCount, Is.EqualTo(2), "Did not get the custom cache key the expected amount of times");
            Assert.That(policy.EnforceCallCount, Is.EqualTo(1), "Did not call enforce the expected amount of times");
        }
开发者ID:pvivera,项目名称:FluentSecurity,代码行数:29,代码来源:PolicyContainerSpec.cs

示例10: PolicyContainer

        public void Should_add_policyresult_cache_strategy_for_Policy_T_with_lifecycle_set_to_PerHttpSession_and_level_set_to_Policy()
        {
            const By expectedLevel = By.Policy;
            var policyContainer = new PolicyContainer("Controller", "Action", TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer.AddPolicy<RequireAnyRolePolicy>().CachePerHttpSession(expectedLevel);

            // Assert
            var policyResultCacheStrategy = policyContainer.CacheStrategies.Single();
            Assert.That(policyResultCacheStrategy.PolicyType, Is.EqualTo(typeof(RequireAnyRolePolicy)));
            Assert.That(policyResultCacheStrategy.CacheLifecycle, Is.EqualTo(Cache.PerHttpSession));
            Assert.That(policyResultCacheStrategy.CacheLevel, Is.EqualTo(expectedLevel));
        }
开发者ID:pvivera,项目名称:FluentSecurity,代码行数:14,代码来源:PolicyContainerSpec.cs

示例11: Should_add_policyresult_cache_strategy_for_Policy_T_with_lifecycle_set_to_PerHttpSession

        public void Should_add_policyresult_cache_strategy_for_Policy_T_with_lifecycle_set_to_PerHttpSession()
        {
            const Cache expectedLifecycle = Cache.PerHttpSession;
            const string expectedControllerName = "Controller3";
            const string expectedActionName = "Action3";

            var policyContainer = new PolicyContainer(expectedControllerName, expectedActionName, TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer.AddPolicy<RequireAnyRolePolicy>().CachePerHttpSession();

            // Assert
            var policyResultCacheStrategy = policyContainer.CacheStrategies.Single();
            Assert.That(policyResultCacheStrategy.ControllerName, Is.EqualTo(expectedControllerName));
            Assert.That(policyResultCacheStrategy.ActionName, Is.EqualTo(expectedActionName));
            Assert.That(policyResultCacheStrategy.PolicyType, Is.EqualTo(typeof(RequireAnyRolePolicy)));
            Assert.That(policyResultCacheStrategy.CacheLifecycle, Is.EqualTo(expectedLifecycle));
            Assert.That(policyResultCacheStrategy.CacheLevel, Is.EqualTo(By.ControllerAction));
        }
开发者ID:pvivera,项目名称:FluentSecurity,代码行数:19,代码来源:PolicyContainerSpec.cs

示例12: SetUp

 public void SetUp()
 {
     // Arrange
     _policyContainer = TestDataFactory.CreateValidPolicyContainer();
     _policyContainer.SecurityConfigurationProvider = TestDataFactory.CreateValidSecurityConfiguration;
     _policyContainer
         .AddPolicy(_policy1)
         .AddPolicy(_policy2);
 }
开发者ID:Ridermansb,项目名称:FluentSecurity,代码行数:9,代码来源:PolicyContainerSpec.cs

示例13: Should_return_the_same_results

        public void Should_return_the_same_results()
        {
            // Arrange
            var context = TestDataFactory.CreateSecurityContext(false);
            var firstPolicy = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.SecurityConfigurationProvider = () => TestDataFactory.CreateValidSecurityConfiguration(configuration => configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.PerHttpRequest));
            policyContainer.AddPolicy(firstPolicy);

            // Act
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            SecurityCache.ClearCache(Lifecycle.HybridHttpContext);;

            var results3 = policyContainer.EnforcePolicies(context);
            var results4 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
            Assert.That(results3.Single(), Is.EqualTo(results4.Single()));

            Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across requests.");
        }
开发者ID:Ridermansb,项目名称:FluentSecurity,代码行数:24,代码来源:PolicyContainerSpec.cs

示例14: Should_return_unique_results

        public void Should_return_unique_results()
        {
            // Arrange
            var firstPolicy = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.SecurityConfigurationProvider = () => TestDataFactory.CreateValidSecurityConfiguration(configuration => configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.DoNotCache));
            policyContainer.AddPolicy(firstPolicy);

            // Act
            var context = TestDataFactory.CreateSecurityContext(false);
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.Not.EqualTo(results2.Single()));
        }
开发者ID:Ridermansb,项目名称:FluentSecurity,代码行数:16,代码来源:PolicyContainerSpec.cs


注:本文中的PolicyContainer.AddPolicy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。